[英]How to make has_many :through association with fixtures?
I can't use factory_girl
because I'm testing sunspot and need real database. 我不能使用factory_girl
因为我正在测试太阳黑子,需要真正的数据库。
Edit : nope. 编辑 :不。 It can works with sunspot. 它可以与太阳黑子一起使用。 I'm wrong. 我错了。
How can I build has_many :through(aka many-to-many) associations in fixtures? 我如何在灯具中构建has_many:through(又名多对多)关联?
I google it and get a invalid solution 我谷歌并得到一个无效的解决方案
Edit : 编辑 :
Finally I use factory_girl. 最后我使用factory_girl。 I google-copy-paste a snippet: 我google-copy-paste一个片段:
factory :tagging do
question { |a| a.association(:question) }
tag { |a| a.association(:tag) }
end
(question has_many tags through taggings, vice versa) (问题has_many标签通过标签,反之亦然)
It works well. 它运作良好。 But what's it? 但它是什么? The factory_girl's readme didn't meantion this syntax. factory_girl的自述文件并不意味着这种语法。 Could someone explain? 有人能解释一下吗
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 : 如果它是一个经典的has_and_belongs_to_many关联,在关联模型中没有其他信息,我认为这些约定允许你像这样编写你的灯具:
#users.yml
john:
first_name: John
last_name: Doe
hobbies: [swim, play_tennis]
#hobbies.yml
swim:
name: Swim
play_tennis:
name: Play Tennis
But I'm not completely sure ! 但我不完全确定!
You can find the official documentation for factory_girl, which is very complete, here . 你可以找到factory_girl的官方文档,功能非常齐全, 在这里 。
Here is a nice (shorter) blogpost explaining factory_girl 2 (comparing it with factory-girl 1). 这是一个很好的(较短的)博客文章解释factory_girl 2(与工厂女孩1比较)。
UPDATED: 更新:
To Explain the code a bit: 稍微解释一下代码:
factory :tagging do
association :tag
end
will look for a factory called :tag
and will construct that object, and then link that to the association tag
(eg a belongs_to
) that is there inside your object :tagging
. 将寻找一个名为:tag
的工厂并构造该对象,然后将其链接到对象内部的关联tag
(例如, belongs_to
) :tagging
。
Please note: this is the default factory. 请注意:这是默认工厂。 If you want taggings
to share a tag
, you will need to do something like 如果你想taggings
共享一个tag
,你需要做的是这样
@tag = Factory(:tag)
@tagging_1 = Factory(:tagging, :tag => @tag)
@tagging_2 = Factory(:tagging, :tag => @tag)
Hope this helps. 希望这可以帮助。
I used fixtures on testing for has_many :through
by hash merge
我使用fixtures测试has_many :through
哈希merge
# posts.yml
one:
title: "Railscasts"
url: "http://railscasts.com/"
description: "Ruby on Rails screencasts"
# categories.yml
one:
name: "magazine"
two:
name: "tutorial"
three:
name: "news"
four:
name: "Ruby"
# posts_controller_test.rb
def test_post_create
assert_difference 'Post.count' do
post :create, post: posts(:one).attributes
.merge(categories: [categories(:two), categories(:four)])
end
end
when after adding another fixture file, and tried this it didn't work 在添加另一个夹具文件后,尝试了这个它没有用
# post_categories.yml
one:
post: one
category: two
two:
post: one
category: four
def test_post_create
assert_difference 'Post.count' do
post :create, post: posts(:one)
end
end
puts posts(:one).attributes
# {"id"=>980190962, "url"=>"http://railscasts.com/", "title"=>"Railscasts", "description"=>"Ruby on Rails screencasts", "created_at"=>Thu, 14 May 2015 18:27:20 UTC +00:00, "updated_at"=>Thu, 14 May 2015 18:27:20 UTC +00:00}
puts posts(:one).attributes
.merge(categories: [categories(:two), categories(:four)])
# {"id"=>980190962, "url"=>"http://railscasts.com/", "title"=>"Railscasts", "description"=>"Ruby on Rails screencasts", "created_at"=>Thu, 14 May 2015 18:30:23 UTC +00:00, "updated_at"=>Thu, 14 May 2015 18:30:23 UTC +00:00, "category_ids"=>[980190962, 1018350795]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.