如何通过与装置关联来制作 has_many :?
我无法使用 factory_girl
因为我正在测试太阳黑子并且需要真实的数据库。
编辑:不。它可以与太阳黑子一起使用。我错了。
如何在灯具中构建 has_many :through(又名多对多)关联?
我用谷歌搜索并得到一个无效的解决方案
编辑:
最后我使用factory_girl。我谷歌复制粘贴一个片段:(
factory :tagging do
question { |a| a.association(:question) }
tag { |a| a.association(:tag) }
end
通过标记询问 has_many 标签,反之亦然)
它效果很好。但那是什么? Factory_girl 的自述文件并不意味着这种语法。 有人可以解释一下吗?
I can't use factory_girl
because I'm testing sunspot and need real database.
Edit: nope. It can works with sunspot. I'm wrong.
How can I build has_many :through(a.k.a many-to-many) associations in fixtures?
I google it and get a invalid solution
Edit:
Finally I use factory_girl. I google-copy-paste a snippet:
factory :tagging do
question { |a| a.association(:question) }
tag { |a| a.association(:tag) }
end
(question has_many tags through taggings, vice versa)
It works well. But what's it? The factory_girl's readme didn't meantion this syntax.
Could someone explain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在此处找到factory_girl的官方文档,该文档非常完整。
这里是一篇不错的(较短的)博文,解释了factory_girl 2(将其与factory-进行比较)女孩1)。
更新:
稍微解释一下代码:
将查找一个名为
:tag
的工厂并构造该对象,然后将其链接到关联tag
(例如tag
>belongs_to) 位于您的对象:tagging
内。请注意:这是默认工厂。如果您希望
taggings
共享一个tag
,您将需要执行类似希望这会有所帮助的操作。
You can find the official documentation for factory_girl, which is very complete, here.
Here is a nice (shorter) blogpost explaining factory_girl 2 (comparing it with factory-girl 1).
UPDATED:
To Explain the code a bit:
will look for a factory called
:tag
and will construct that object, and then link that to the associationtag
(e.g. abelongs_to
) that is there inside your object:tagging
.Please note: this is the default factory. If you want
taggings
to share atag
, you will need to do something likeHope this helps.
如果它是一个经典的 has_and_belongs_to_many 关联,并且关联模型中没有其他信息,我认为约定允许您这样编写固定装置:
但我不完全确定!
If it's a classic has_and_belongs_to_many association, without other information in the association model, I think the conventions allow you to write your fixtures like that :
But I'm not completely sure !
添加另一个固定文件后,我使用固定装置通过哈希
merge
测试has_many :through
,并尝试这样做,但它不起作用
I used fixtures on testing for
has_many :through
by hashmerge
when after adding another fixture file, and tried this it didn't work