工厂女工协会
我编写了一个 rspec 测试,它在我的标题中添加了一些单元。
我有两个型号=>标题和单位。量规有很多单位。 看起来像这样:
@rubric.units.push Factory :text_unit
@rubric.save
然后我找到factory_girl并尝试重写 将此代码作为工厂。但它不起作用。
我怎样才能在工厂女孩中写出这个关联。 我试过这个:
factory :common_rubric , :class => :common_info_rubric do |f|
f.sequence(:name) {|n| "common_info_rubric#{n}"}
end
factory :text_unit, :class => text_info_unit do |f|
f.association :common_rubric_with_unit
f.sequence(:name) {|n| "text_unit#n}" }
end
factory :common_rubric_with_unit , :parent => :common_rubric do |f|
f.units { |unit| unit.association(:text_info_unit) }
end
我总是有错误
SystemStackError:
stack level too deep
I've written a rspec test which adds into my rubric some units.
I have two models => Rubric and Units. Rubrics have many units.
It looks like this:
@rubric.units.push Factory :text_unit
@rubric.save
Then I found factory_girl and tried to rewrite
this code as a factory. But it is not working.
How can i write this association in Factory Girl.
I tried this:
factory :common_rubric , :class => :common_info_rubric do |f|
f.sequence(:name) {|n| "common_info_rubric#{n}"}
end
factory :text_unit, :class => text_info_unit do |f|
f.association :common_rubric_with_unit
f.sequence(:name) {|n| "text_unit#n}" }
end
factory :common_rubric_with_unit , :parent => :common_rubric do |f|
f.units { |unit| unit.association(:text_info_unit) }
end
I always have error
SystemStackError:
stack level too deep
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你那里有一个循环引用。当您创建
text_unit
时,它会创建关联的common_rubric_with_unit
。common_rubric_with_unit
的定义创建了一个关联的text_unit
,我们又回到了起点。您需要从任一侧删除一个关联,这应该有效:
You have a circular reference there. When you create a
text_unit
it creates an associatedcommon_rubric_with_unit
. The definition forcommon_rubric_with_unit
creates an associatedtext_unit
and we're back at the start.You'll need to remove one of the associations from either side, this should work:
所有问题都是模型中表的非默认名称。
读完后
http://robots.thoughtbot.com/post/254496652/aint -无马蹄莲背女孩
我解决问题
All problem was the undefault name of table in models.
And after read
http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-girl
i solve the problem