On Feb 14, 2009, at 2:37 PM, Phlip wrote: > John Smith wrote: > >> I use new in almost all my controllers, but sometimes I have seen the >> build method inside the controllers of some apps. Can someone explain >> when should I use build? > > .build fixes the fact you cannot say this: > > my_post.tags.new(:name => 'reggae') > > The new() operator is special, and your editor might color it > different. So the > architects of ActiveRecord use .build() instead. > > Both create a new object. But shouldn't we just use this? > > my_post.tags.create(:name => 'reggae') > my_post.tags.create!(:name => 'reggae') > > What do they do? > > -- > Phlip
In particular, #build (and #create) automatically set, in this case, the :post_id of the associated Tag object to be my_post.id There are times when you want to work briefly with an unsaved object and #build is more appropriate than #create -Rob Rob Biedenharn http://agileconsultingllc.com r...@agileconsultingllc.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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---