gpt4 book ai didi

ruby-on-rails - Rails 工厂女孩与圆形关系

转载 作者:行者123 更新时间:2023-12-01 06:29:30 25 4
gpt4 key购买 nike

我有两个具有循环关系的模型。我如何在 Factory Girl 中为这些建模?

我收到太多错误消息,正在尝试执行此操作。

楷模:

class Account < ActiveRecord::Base
has_many :users
belongs_to :owner, class_name: "User", inverse_of: :account
end

class User < ActiveRecord::Base
belongs_to :account
end

工厂:
FactoryGirl.define do
factory :user, aliases: [:owner] do
sequence(:email) { |n| "user#{n}@example.com" }
password "test"
password_confirmation "test"
account
end
end

FactoryGirl.define do
factory :account do |account|
account.name "My School"
account.short_name "school1"
account.sequence(:subdomain) { |n| "school#{n}" }
account.owner { FactoryGirl.build(:user, account: account) }
end
end

我收到以下错误:
 Failure/Error: let!(:user) { FactoryGirl.create(:user) }
NoMethodError:
undefined method `primary_key' for #<FactoryGirl::Declaration::Implicit:0x00000007bd9050>
# ./spec/factories/accounts.rb:8:in `block (3 levels) in <top (required)>'
# ./spec/models/user_spec.rb:8:in `block (3 levels) in <top (required)>'
# -e:1:in `<main>'

你能帮忙解决这个错误吗?

最佳答案

尝试这个

FactoryGirl.define do
factory :user do
sequence(:email) { |n| "user#{n}@example.com" }
password "test"
password_confirmation "test"
association :account
end
end

FactoryGirl.define do
factory :account do
name "My School"
short_name "school1"
sequence(:subdomain) { |n| "school#{n}" }
association :owner, factory: :user
end
end

关于ruby-on-rails - Rails 工厂女孩与圆形关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24115483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com