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

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

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:

  1. 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:

  1. user = User.new(
  2. first_name: 'Phil',
  3. last_name: '6',
  4. email: 'phil@email.domain',
  5. super_user: true,
  6. password: 'test1234'
  7. )
  8. user.skip_confirmation!
  9. user.save!
  10. organisation = Organisation.find_by_name('Team')
  11. Membership.create!(
  12. user:,
  13. organisation:,
  14. verified: true,
  15. verified_at: now,
  16. organisation_admin: true,
  17. shift_admin: true,
  18. email: 'phil.6@group.com',
  19. email_confirmed: true,
  20. category: organisation.categories.find_by_name('Developer')
  21. )
  22. organisation = Organisation.find_by_name('Test Org')
  23. membership = Membership.create!(
  24. user:,
  25. organisation:,
  26. verified: true,
  27. verified_at: now,
  28. email: 'phil@testorg.com',
  29. email_confirmed: true
  30. )

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:

  1. #<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">
  2. #<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:

  1. 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:

  1. user = User.new(
  2. first_name: &#39;Phil&#39;,
  3. last_name: &#39;6&#39;,
  4. email: &#39;phil@email.domain&#39;,
  5. super_user: true,
  6. password: &#39;test1234&#39;
  7. )
  8. user.skip_confirmation!
  9. user.save!
  10. organisation = Organisation.find_by_name(&#39;Team&#39;)
  11. Membership.create!(
  12. user:,
  13. organisation:,
  14. verified: true,
  15. verified_at: now,
  16. organisation_admin: true,
  17. shift_admin: true,
  18. email: &#39;phil.6@group.com&#39;,
  19. email_confirmed: true,
  20. category: organisation.categories.find_by_name(&#39;Developer&#39;)
  21. )
  22. organisation = Organisation.find_by_name(&#39;Test Org&#39;)
  23. membership = Membership.create!(
  24. user:,
  25. organisation:,
  26. verified: true,
  27. verified_at: now,
  28. email: &#39;phil@testorg.com&#39;,
  29. email_confirmed: true
  30. )

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:

  1. #&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;
  2. #&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

以下是翻译好的内容:

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

  1. class User < ApplicationRecord
  2. has_many :memberships
  3. end

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

  1. user = User.create!(
  2. first_name: 'Phil',
  3. last_name: '6',
  4. email: 'phil@email.domain',
  5. super_user: true,
  6. password: 'test1234',
  7. confirmed_at: Time.current # 略过 Devise::Confirmable 的简便方法
  8. )
  9. # 确保你使用感叹号方法,以便不仅仅得到一个空值
  10. organisation = Organisation.find_by_name!('Test Org')
  11. user.memberships.create!(
  12. organisation: organisation,
  13. verified: true,
  14. verified_at: now,
  15. organisation_admin: true,
  16. shift_admin: true,
  17. email: 'phil.6@group.com',
  18. email_confirmed: true,
  19. category: organisation.categories.find_by_name!('Developer')
  20. )
英文:

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

  1. class User &lt; ApplicationRecord
  2. has_many :memberships
  3. end

Then you create the memberships through that assocation instead:

  1. user = User.create!(
  2. first_name: &#39;Phil&#39;,
  3. last_name: &#39;6&#39;,
  4. email: &#39;phil@email.domain&#39;,
  5. super_user: true,
  6. password: &#39;test1234&#39;,
  7. confirmed_at: Time.current # the easy way to skip Devise::Confirmable
  8. )
  9. # make sure you use the bang method so that you&#39;re not just getting a nil
  10. organisation = Organisation.find_by_name!(&#39;Test Org&#39;)
  11. user.memberships.create!(
  12. organisation: organisation,
  13. verified: true,
  14. verified_at: now,
  15. organisation_admin: true,
  16. shift_admin: true,
  17. email: &#39;phil.6@group.com&#39;,
  18. email_confirmed: true,
  19. category: organisation.categories.find_by_name!(&#39;Developer&#39;)
  20. )

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:

确定