Ruby on Rails Friday, September 3, 2010

Hello,

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.
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment