工厂女孩有很多关系(和受保护的属性)
我有这种关系:
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受保护属性,这有点棘手。所以我认为如果我在文章工厂内声明关联应该会更好,但我不知道如何处理。
感谢您的任何帮助。
I have this kind of relation:
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")
I had tried to write those factories:
Factory.define :article do |f|
f.title "Hello, world"
end
Factory.define :comment do |f|
f.content "Awesome!"
f.association :article
end
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.
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你应该做
You should do