Joerg Diekmann
2006-Jan-15 11:19 UTC
[Rails] Saving one-many associations (elegant solution please)
What is the most elegant way to save new one-many associations? order = Order.new(:name => "My Order") order.line_items << LineItem.new(:product_id => 1, :quantity => 2) order.line_items << LineItem.new(:product_id => 2, :quantity => 5) order.save The above - which is by far the most elegant way of putting it - doesn''t work for me - the line_items don''t get saved. Presumably because the order does not have an id yet, so they''re not linked up. What is the best way to deal with such one-many associations when creating new ones, other than first saving the order, then adding the line_items and saving the order again? Thanks Joerg -- Posted via http://www.ruby-forum.com/.
Marcel Molina Jr.
2006-Jan-15 12:05 UTC
[Rails] Saving one-many associations (elegant solution please)
On Sun, Jan 15, 2006 at 12:19:01PM +0100, Joerg Diekmann wrote:> What is the most elegant way to save new one-many associations? > > order = Order.new(:name => "My Order") > order.line_items << LineItem.new(:product_id => 1, :quantity => 2) > order.line_items << LineItem.new(:product_id => 2, :quantity => 5) > order.save > > The above - which is by far the most elegant way of putting it - doesn''t > work for me - the line_items don''t get saved. Presumably because the > order does not have an id yet, so they''re not linked up. > > What is the best way to deal with such one-many associations when > creating new ones, other than first saving the order, then adding the > line_items and saving the order again?You can use build. order = Order.new(:name => ''My Order'') order.line_items.build(:product_id => 1, :quantity => 2) order.line_items.build(:product_id => 2, :quantity => 5) order.save marcel -- Marcel Molina Jr. <marcel@vernix.org>
Joerg Diekmann
2006-Jan-15 14:32 UTC
[Rails] Re: Saving one-many associations (elegant solution please)
Ah of course. I always misread the Agile Rails book for some reason. Thanks. -- Posted via http://www.ruby-forum.com/.
Ben Munat
2006-Jan-15 19:14 UTC
[Rails] Saving one-many associations (elegant solution please)
Marcel Molina Jr. wrote:> > You can use build. > > order = Order.new(:name => ''My Order'') > order.line_items.build(:product_id => 1, :quantity => 2) > order.line_items.build(:product_id => 2, :quantity => 5) > order.save >What class/module is "build" in? Can''t find it anywhere in the rails api... b
Kevin Olbrich
2006-Jan-15 19:24 UTC
[Rails] Re: Saving one-many associations (elegant solution please)
Ben Munat wrote:> What class/module is "build" in? Can''t find it anywhere in the rails > api... > > bLook at the description of has_many in the api. It adds several methods to your collection, ''build'' is one of them. For some reason it doesn''t show up by itself in the API. _Kevin -- Posted via http://www.ruby-forum.com/.
Justin Forder
2006-Jan-16 06:21 UTC
[Rails] Re: Saving one-many associations (elegant solution please)
Kevin Olbrich wrote:> Ben Munat wrote: >> What class/module is "build" in? Can''t find it anywhere in the rails >> api... >> >> b > > Look at the description of has_many in the api. It adds several methods > to your collection, ''build'' is one of them. For some reason it doesn''t > show up by itself in the API.RDoc is limited to documenting the static structure of a program - i.e. the code that is there in the source files. Rails uses a combination of adding methods to application classes at runtime, and interpreting method calls on the fly using method_missing. Features that are implemented in this way have to be documented in the narrative text for a class, and can''t appear in the RDoc-generated method index. regards Justin
Sean Hussey
2006-Feb-03 22:20 UTC
[Rails] Saving one-many associations (elegant solution please)
Is there a similar way to do this with HABTM relationships? I don''t think build is included. On 1/15/06, Marcel Molina Jr. <marcel@vernix.org> wrote:> On Sun, Jan 15, 2006 at 12:19:01PM +0100, Joerg Diekmann wrote: > > What is the most elegant way to save new one-many associations? > > > > order = Order.new(:name => "My Order") > > order.line_items << LineItem.new(:product_id => 1, :quantity => 2) > > order.line_items << LineItem.new(:product_id => 2, :quantity => 5) > > order.save > > > > The above - which is by far the most elegant way of putting it - doesn''t > > work for me - the line_items don''t get saved. Presumably because the > > order does not have an id yet, so they''re not linked up. > > > > What is the best way to deal with such one-many associations when > > creating new ones, other than first saving the order, then adding the > > line_items and saving the order again? > > You can use build. > > order = Order.new(:name => ''My Order'') > order.line_items.build(:product_id => 1, :quantity => 2) > order.line_items.build(:product_id => 2, :quantity => 5) > order.save > > marcel > -- > Marcel Molina Jr. <marcel@vernix.org> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >