工厂女孩有很多关系(和受保护的属性)

我有这种关系:

class Article < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :article attr_protected :article_id end 

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

 @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属性,这有点棘手。 所以我认为如果我在文章工厂内声明关联应该会更好,但我不知道如何处理。

谢谢你的帮助。

你应该做

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