如何通过与装置关联来制作 has_many :?

发布于 2024-12-03 15:26:45 字数 568 浏览 3 评论 0原文

我无法使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

早茶月光 2024-12-10 15:26:45

您可以在此处找到factory_girl的官方文档,该文档非常完整。

这里是一篇不错的(较短的)博文,解释了factory_girl 2(将其与factory-进行比较)女孩1)。

更新:

稍微解释一下代码:

 factory :tagging do
   association :tag
 end

将查找一个名为 :tag 的工厂并构造该对象,然后将其链接到关联 tag (例如 tag >belongs_to) 位于您的对象 :tagging 内。

请注意:这是默认工厂。如果您希望taggings共享一个tag,您将需要执行类似

@tag = Factory(:tag)
@tagging_1 = Factory(:tagging, :tag => @tag)
@tagging_2 = Factory(:tagging, :tag => @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:

 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 (e.g. a belongs_to) that is there inside your object :tagging.

Please note: this is the default factory. If you want taggings to share a tag, you will need to do something like

@tag = Factory(:tag)
@tagging_1 = Factory(:tagging, :tag => @tag)
@tagging_2 = Factory(:tagging, :tag => @tag)

Hope this helps.

触ぅ动初心 2024-12-10 15:26:45

如果它是一个经典的 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

但我不完全确定!

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 :

#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 !

风苍溪 2024-12-10 15:26:45

添加另一个固定文件后,我使用固定装置通过哈希 merge 测试 has_many :through

# 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

并尝试这样做,但它不起作用

# 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]}

I used fixtures on testing for has_many :through by hash 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]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文