Forum: Ruby on Rails Saving one-many associations (elegant solution please)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
34791e1d8c4def65f9b93294638a7e04?d=identicon&s=25 Joerg Diekmann (joergd)
on 2006-01-15 12:19
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
Cee0292fffa691f1fb320d5400200e99?d=identicon&s=25 Marcel Molina Jr. (Guest)
on 2006-01-15 13:07
(Received via mailing list)
On Sun, Jan 15, 2006 at 12:19:01PM +0100, Joerg Diekmann wrote:
>
> 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
34791e1d8c4def65f9b93294638a7e04?d=identicon&s=25 Joerg Diekmann (joergd)
on 2006-01-15 15:32
Ah of course. I always misread the Agile Rails book for some reason.
Thanks.
4005a47a8f2ceee49670b920593c1d52?d=identicon&s=25 Ben Munat (Guest)
on 2006-01-15 20:17
(Received via mailing list)
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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-15 20:24
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.

_Kevin
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-01-16 07:22
(Received via mailing list)
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
2b2c2a705ed12f8fb327c7b4c56456c6?d=identicon&s=25 Sean Hussey (seanhussey)
on 2006-02-03 23:21
(Received via mailing list)
Is there a similar way to do this with HABTM relationships?  I don't
think build is included.
This topic is locked and can not be replied to.