andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-23 14:23 UTC
Problem creating records through association using .new
Hello, When I do order = Order.find :first line_item = order.line_items.new line_item.save the LineItem object is saved with order_id = nil. However, if I just do order.line_item.create, order_id is filled out correctly and it saves the association. Why doesn''t it work with .new? Thanks, Andrew --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jason Roelofs
2007-Apr-23 14:28 UTC
Re: Problem creating records through association using .new
Well, because it doesn''t work that way. #create saves, #new doesn''t. Foreign keys are always filled in at save time. Jason On 4/23/07, andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Hello, > > When I do > > order = Order.find :first > line_item = order.line_items.new > line_item.save > > the LineItem object is saved with order_id = nil. > > However, if I just do order.line_item.create, order_id is filled out > correctly and it saves the association. > > Why doesn''t it work with .new? > > Thanks, > > Andrew > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Brian Hogan
2007-Apr-23 14:31 UTC
Re: Problem creating records through association using .new
@Jason: I think he means the Foreign key is nil (order_id in the line_item class should have been filled in for him because of the association.) @Andrew: Collection#new doesn''t work that way... in fact if you look at the API for has_many you won''t even see a ''new'' method. You want ''build'' order.line_items.build (:thing => other_thing.... } (I hate that syntax... new makes more sense there than build. ) On 4/23/07, Jason Roelofs <jameskilton-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Well, because it doesn''t work that way. #create saves, #new doesn''t. > > Foreign keys are always filled in at save time. > > Jason > > On 4/23/07, andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Hello, > > > > When I do > > > > order = Order.find :first > > line_item = order.line_items.new > > line_item.save > > > > the LineItem object is saved with order_id = nil. > > > > However, if I just do order.line_item.create , order_id is filled out > > correctly and it saves the association. > > > > Why doesn''t it work with .new? > > > > Thanks, > > > > Andrew > > > > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---