与工厂女工有很多关系(和受保护的属性) [英] Factory Girl with has many relationship (and a protected attribute)

查看:60
本文介绍了与工厂女工有很多关系(和受保护的属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种关系:

class Article < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :article
  attr_protected :article_id
end

控制器内部的默认方案如下:

The default scenario inside controllers looks like:

@article = Article.create(:title => "foobar")
@comment = @article.comments.create(:content => "w00t")

我试图写那些工厂:

Factory.define :article do |f|
  f.title "Hello, world"
end

Factory.define :comment do |f|
  f.content "Awesome!"
  f.association :article
end

但是我的语法关于关联是不正确的.由于评论的article_id protected属性,这有点棘手.因此,如果我在商品工厂内声明关联,则我认为这样做会更好,但是我看不到如何处理.

But my syntax is not correct about the association. It's a little bit tricky because of the comment's article_id protected attribute. So I think this should be better if I declare the association inside the article factory, but I don't see how to process.

感谢您的帮助.

推荐答案

您应该这样做

Factory.define :comment do |f|
  f.content "Awesome!"
  f.article { |a| a.association(:article) }
end

这篇关于与工厂女工有很多关系(和受保护的属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆