简体   繁体   English

factory_girl 创建与构建和关联

[英]factory_girl create vs build and associations

I am having some trouble with factory_girl's associations:我在 factory_girl 的关联方面遇到了一些问题:

Factory.define :account do |f|
  f.sequence(:email) {|n| "john#{n}@example.com" }
  f.password "secret"
  f.confirmed_at 2.days.ago.to_s(:db)
  f.name "John Doe"
  f.seller false
  f.admin false
end

Factory.define :request do |f|
  f.association :account
  f.message 'Test message.'
end

And here's the problem:这就是问题所在:

When I do a = Factory(:request), a.account => nil but when I do a = Factory.build(:request), a.account is correct.当我执行 a = Factory(:request), a.account => nil 但当我执行 a = Factory.build(:request) 时,a.account 是正确的。

Any idea why this might be?知道为什么会这样吗?

Thanks谢谢

Factory(:request) calls for default strategy, did you changed that somewhere? Factory(:request) 调用默认策略,您是否在某个地方更改了它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM