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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Fri, Sep 3, 2010 at 6:07 AM, Satsou Sa <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 > endI''m still pretty newb, so I could be way off here, but I think you need to define the factory that represents the association? Factory.define :comment do |f| f.content "Awesome!" f.association :article, :factory => :article end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On Fri, Sep 3, 2010 at 8:38 AM, Rick R <rickcr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Fri, Sep 3, 2010 at 6:07 AM, Satsou Sa <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> 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 > > I''m still pretty newb, so I could be way off here, but I think you > need to define the factory that represents the association? > > Factory.define :comment do |f| > f.content "Awesome!" > f.association :article, :factory => :article > end >It''s also a bit confusing because your naming your factories the same as the property name. It might be more clear if you set them with more ''real'' names like Factory.define :hello_world_article, :class => Article do |f| f.title "Hello, world" end Factory.define :awesome_comment, :class => Comment do |f| f.content "Awesome!" f.association :article, :factory => :hello_world_article end -- Rick R -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Try this Factory.define :article do |f| f.title "Hello, world" f.comments {|comments| [comments.association(::comment)]} end Factory.define :comment do |f| f.content "Awesome!" end On Sep 3, 6:07 am, Satsou Sa <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Many thanks for your help. Very appreciated. Happy RoR3 developments! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.