Rails give us two ways to create a asociation object: @article = Article.new(params[:article]) @article.comments << Comment.new(params[:comment]) @article.save! or @article = Article.new(params[:article]) @comment = @article.comments.build(params[:comment]) @article.save! @comment.save! what''s the advantage of using build and << ?? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
<< should save @article when you add a new comment. ----- Ryan Bigg Freelancer http://frozenplague.net On 24/12/2008, at 3:02 PM, Zhenning Guan wrote:> > Rails give us two ways to create a asociation object: > @article = Article.new(params[:article]) > @article.comments << Comment.new(params[:comment]) > @article.save! > or > > @article = Article.new(params[:article]) > @comment = @article.comments.build(params[:comment]) > @article.save! > @comment.save! > > what''s the advantage of using build and << ?? > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
<< adds records to a association-collection (no matter what kind of association). it returns itself (the collection) so method calls may be chained. btw: in this example @article doesn''t have to be saved when a new comment is added (comment has article_id as foreign key). ;-) --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
just saw that this could be misunderstood: article has to be saved to be able to make a association, but it doesn''t have to be saved after adding a new comment. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
MaD wrote:> << adds records to a association-collection (no matter what kind of > association). it returns itself (the collection) so method calls may > be chained. > > btw: in this example @article doesn''t have to be saved when a new > comment is added (comment has article_id as foreign key). ;-)what''s does that -method calls may be chained-? can you make me a concrete example? in this example @article doesn''t have to be saved when a new> comment is added (comment has article_id as foreign key)@article = Article.new(params[:article]) don''t we have to save it for store a Article instance? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---