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.
Joerg D. (Guest)
on 2006-01-15 13: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
Marcel Molina Jr. (Guest)
on 2006-01-15 14:07
(Received via mailing list)
On Sun, Jan 15, 2006 at 12:19:01PM +0100, Joerg D. 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
Joerg D. (Guest)
on 2006-01-15 16:32
Ah of course. I always misread the Agile Rails book for some reason.
Thanks.
Ben M. (Guest)
on 2006-01-15 21: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
Kevin O. (Guest)
on 2006-01-15 21:24
Ben M. 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
Justin F. (Guest)
on 2006-01-16 08:22
(Received via mailing list)
Kevin O. wrote:
> Ben M. 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 H. (Guest)
on 2006-02-04 00: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.