ruby-on-rails 活动记录::关联类型不匹配:预期用户(#35560),得到...这是用户(#22700)的示例

plupiseo  于 2023-02-10  发布在  Ruby
关注(0)|答案(1)|浏览(128)

我在用rails播种的时候遇到了一些间歇性的错误,我希望有人能帮助我了解User类的不同类型。
错误全文如下:

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)

导致此问题的代码:

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
  )

如果我在出现错误之前暂停执行,我可以看到user == User.first为false,尽管User.firstuser是这两行,它们在视觉上是相同的:

#<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">

如果我比较user.classUser.first.class,情况也是一样的,它们看起来是一样的,但是比较的结果是false。
我是不是做了一些改变局部变量的事情?

uujelgoq

uujelgoq1#

您应该在这里做的是创建一个关联:

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 # the easy way to skip Devise::Confirmable
  )

  # make sure you use the bang method so that you're not just getting a nil
  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')
  )

相关问题