Ruby on rails 如何为具有has_one/belling_to关系的模型创建一个工厂,该关系通常由嵌套属性克服验证?
我有一个帐户模型,它有一个用户模型和一个属于帐户模型的用户模型。我认为演示所需的基本代码是:Ruby on rails 如何为具有has_one/belling_to关系的模型创建一个工厂,该关系通常由嵌套属性克服验证?,ruby-on-rails,validation,factory-bot,Ruby On Rails,Validation,Factory Bot,我有一个帐户模型,它有一个用户模型和一个属于帐户模型的用户模型。我认为演示所需的基本代码是: class Account < ActiveRecord::Base has_one :user validates_presence_of :user accepts_nested_attributes_for :user end class User < ActiveRecord::Base belongs_to :account # validates_prese
class Account < ActiveRecord::Base
has_one :user
validates_presence_of :user
accepts_nested_attributes_for :user
end
class User < ActiveRecord::Base
belongs_to :account
# validates_presence_of :account # this is not actually present,
# but is implied by a not null requirement
# in the database, so it only takes effect on
# save or update, instead of on #valid?
end
我得到一个堆栈溢出,因为每个堆栈都递归地创建一个帐户/用户
我能够解决这个问题的方法是在测试中模拟嵌套的属性形式:
before :each do
account_attributes = Factory.attributes_for :account
account_attributes[:user_attributes] = Factory.attributes_for :user
@account = Account.new(account_attributes)
end
但是,我希望将此逻辑保留在工厂中,因为一旦我开始添加其他模块,它就会失控:
before :each do
account_attributes = Factory.attributes_for :account
account_attributes[:user_attributes] = Factory.attributes_for :user
account_attributes[:user_attributes][:profile_attributes] = Factory.attributes_for :profile
account_attributes[:payment_profile_attributes] = Factory.attributes_for :payment_profile
account_attributes[:subscription_attributes] = Factory.attributes_for :subscription
@account = Account.new(account_attributes)
end
请帮忙 看一看。你建立这些账户的方式似乎不是真的在利用工厂女孩
我总是通过在测试之前创建所需的对象来处理关联。我将根据您在上面引用的模型对此进行尝试:
before :each do
@account = Factory(:account, :user_id => Factory(:user).id, :profile_id => Factory(:profile).id)
end
现在
@account
将有@account.user
和@account.profile
可用。如果您需要定义它们,@profile=@account.profile
非常有效。我可以通过使用factory\u girl的后构建回调来解决这个问题
Factory.define :account do |f|
f.after_build do |account|
account.user ||= Factory.build(:user, :account => account)
account.payment_profile ||= Factory.build(:payment_profile, :account => account)
account.subscription ||= Factory.build(:subscription, :account => account)
end
end
Factory.define :user do |f|
f.after_build do |user|
user.account ||= Factory.build(:account, :user => user)
user.profile ||= Factory.build(:profile, :user => user)
end
end
这将在保存所属类之前创建关联类,因此验证通过。我遇到的问题是,用户属于account,而不是account,因此account#user#id不存在,而user#account#id是需要设置的属性。但是,如果没有附加了关联用户的帐户,我无法保存帐户以获取id。
user.profile
来自哪里?这是否意味着是user.account
?user.profile是存储与帐户关联的用户信息的模型。因此,结构是Account>User>Profile。不过,我添加了user.account,因为我认为如果直接创建用户,这是必要的。
Factory.define :account do |f|
f.after_build do |account|
account.user ||= Factory.build(:user, :account => account)
account.payment_profile ||= Factory.build(:payment_profile, :account => account)
account.subscription ||= Factory.build(:subscription, :account => account)
end
end
Factory.define :user do |f|
f.after_build do |user|
user.account ||= Factory.build(:account, :user => user)
user.profile ||= Factory.build(:profile, :user => user)
end
end