ActiveRecord::AssociationTypeMismatch: User(#35560) expected, got … which is an instance of User(#22700)

huangapple go评论65阅读模式
英文:

ActiveRecord::AssociationTypeMismatch: User(#35560) expected, got ... which is an instance of User(#22700)

问题

I've been getting intermittent errors while seeding with rails. I'm hoping someone can help provide some insight into the different types of the User class.

The error in full:

ActiveRecord::AssociationTypeMismatch: User(#35560) expected, got #<User id: "bedc7c4e-cdd2-4ea1-a7ee-4e6642467fba", email: "phil@email.domain", jti: "7376cf41-7f88-407d-8365-1e311d946b88", ios_device_token: nil, fcm_device_token: nil, first_name: "Phil", last_name: "6", phone_number: nil, date_of_birth: nil, super_user: true, created_at: "2023-02-08 08:16:37.559974000 +0000", updated_at: "2023-02-08 08:16:37.559974000 +0000"> which is an instance of User(#22700)

The code which causes it:

  user = User.new(
    first_name: 'Phil',
    last_name: '6',
    email: 'phil@email.domain',
    super_user: true,
    password: 'test1234'
  )
  user.skip_confirmation!
  user.save!

  organisation = Organisation.find_by_name('Team')
  Membership.create!(
    user:,
    organisation:,
    verified: true,
    verified_at: now,
    organisation_admin: true,
    shift_admin: true,
    email: 'phil.6@group.com',
    email_confirmed: true,
    category: organisation.categories.find_by_name('Developer')
  )

  organisation = Organisation.find_by_name('Test Org')
  membership = Membership.create!(
    user:,
    organisation:,
    verified: true,
    verified_at: now,
    email: 'phil@testorg.com',
    email_confirmed: true
  )

If I pause execution before the error I can see that user == User.first is false despite User.first and user being these two lines, which are visually identical:

#<User id: "6ce62b08-cf4c-4bfa-878a-02a1ed889c69", email: "phil@email.domain", jti: "710948b6-5f4f-40ea-ab9f-df8e3b1219c3", ios_device_token: nil, fcm_device_token: nil, first_name: "Phil", last_name: "6", phone_number: nil, date_of_birth: nil, super_user: true, created_at: "2023-02-08 08:17:06.024800000 +0000", updated_at: "2023-02-08 08:17:06.024800000 +0000">
#<User id: "6ce62b08-cf4c-4bfa-878a-02a1ed889c69", email: "phil@email.domain", jti: "710948b6-5f4f-40ea-ab9f-df8e3b1219c3", ios_device_token: nil, fcm_device_token: nil, first_name: "Phil", last_name: "6", phone_number: nil, date_of_birth: nil, super_user: true, created_at: "2023-02-08 08:17:06.024800000 +0000", updated_at: "2023-02-08 08:17:06.024800000 +0000">

It's the same thing if I compare user.class and User.first.class, they look the same but a comparison evaluates to false.

Am I doing something to mutate the local variable?

英文:

I've been getting intermittent errors while seeding with rails. I'm hoping someone can help provide some insight into the different types of the User class.

The error in full:

ActiveRecord::AssociationTypeMismatch: User(#35560) expected, got #&lt;User id: &quot;bedc7c4e-cdd2-4ea1-a7ee-4e6642467fba&quot;, email: &quot;phil@email.domain&quot;, jti: &quot;7376cf41-7f88-407d-8365-1e311d946b88&quot;, ios_device_token: nil, fcm_device_token: nil, first_name: &quot;Phil&quot;, last_name: &quot;6&quot;, phone_number: nil, date_of_birth: nil, super_user: true, created_at: &quot;2023-02-08 08:16:37.559974000 +0000&quot;, updated_at: &quot;2023-02-08 08:16:37.559974000 +0000&quot;&gt; which is an instance of User(#22700)

The code which causes it:

  user = User.new(
    first_name: &#39;Phil&#39;,
    last_name: &#39;6&#39;,
    email: &#39;phil@email.domain&#39;,
    super_user: true,
    password: &#39;test1234&#39;
  )
  user.skip_confirmation!
  user.save!

  organisation = Organisation.find_by_name(&#39;Team&#39;)
  Membership.create!(
    user:,
    organisation:,
    verified: true,
    verified_at: now,
    organisation_admin: true,
    shift_admin: true,
    email: &#39;phil.6@group.com&#39;,
    email_confirmed: true,
    category: organisation.categories.find_by_name(&#39;Developer&#39;)
  )

  organisation = Organisation.find_by_name(&#39;Test Org&#39;)
  membership = Membership.create!(
    user:,
    organisation:,
    verified: true,
    verified_at: now,
    email: &#39;phil@testorg.com&#39;,
    email_confirmed: true
  )

If I pause execution before the error I can see that user == User.first is false despite User.first and user being these two lines, which are visually identical:

#&lt;User id: &quot;6ce62b08-cf4c-4bfa-878a-02a1ed889c69&quot;, email: &quot;phil@email.domain&quot;, jti: &quot;710948b6-5f4f-40ea-ab9f-df8e3b1219c3&quot;, ios_device_token: nil, fcm_device_token: nil, first_name: &quot;Phil&quot;, last_name: &quot;6&quot;, phone_number: nil, date_of_birth: nil, super_user: true, created_at: &quot;2023-02-08 08:17:06.024800000 +0000&quot;, updated_at: &quot;2023-02-08 08:17:06.024800000 +0000&quot;&gt;
#&lt;User id: &quot;6ce62b08-cf4c-4bfa-878a-02a1ed889c69&quot;, email: &quot;phil@email.domain&quot;, jti: &quot;710948b6-5f4f-40ea-ab9f-df8e3b1219c3&quot;, ios_device_token: nil, fcm_device_token: nil, first_name: &quot;Phil&quot;, last_name: &quot;6&quot;, phone_number: nil, date_of_birth: nil, super_user: true, created_at: &quot;2023-02-08 08:17:06.024800000 +0000&quot;, updated_at: &quot;2023-02-08 08:17:06.024800000 +0000&quot;&gt;

It's the same thing if I compare user.class and User.first.class, they look the same but a comparison evaluates to false.

Am I doing something to mutate the local variable?

答案1

得分: 1

以下是翻译好的内容:

你应该在这里创建一个关联:

class User < ApplicationRecord
  has_many :memberships
end

然后通过该关联创建成员资格:

user = User.create!(
  first_name: 'Phil',
  last_name: '6',
  email: 'phil@email.domain',
  super_user: true,
  password: 'test1234',
  confirmed_at: Time.current # 略过 Devise::Confirmable 的简便方法
)

# 确保你使用感叹号方法,以便不仅仅得到一个空值
organisation = Organisation.find_by_name!('Test Org')

user.memberships.create!(
  organisation: organisation,
  verified: true,
  verified_at: now,
  organisation_admin: true,
  shift_admin: true,
  email: 'phil.6@group.com',
  email_confirmed: true,
  category: organisation.categories.find_by_name!('Developer')
)
英文:

What you should be doing here is to create an assocation:

class User &lt; ApplicationRecord
  has_many :memberships
end

Then you create the memberships through that assocation instead:

 user = User.create!(
    first_name: &#39;Phil&#39;,
    last_name: &#39;6&#39;,
    email: &#39;phil@email.domain&#39;,
    super_user: true,
    password: &#39;test1234&#39;,
    confirmed_at: Time.current # the easy way to skip Devise::Confirmable
  )

  # make sure you use the bang method so that you&#39;re not just getting a nil
  organisation = Organisation.find_by_name!(&#39;Test Org&#39;) 

  user.memberships.create!(
    organisation: organisation,
    verified: true,
    verified_at: now,
    organisation_admin: true,
    shift_admin: true,
    email: &#39;phil.6@group.com&#39;,
    email_confirmed: true,
    category: organisation.categories.find_by_name!(&#39;Developer&#39;)
  )

huangapple
  • 本文由 发表于 2023年2月8日 16:34:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定