# Saving one-many associations (elegant solution please)

**URL:** <https://www.ruby-forum.com/t/saving-one-many-associations-elegant-solution-please/52501>\
**Category:** Rails\
**Created:** [January 15, 2006, 12:19pm UTC](https://www.ruby-forum.com/t/saving-one-many-associations-elegant-solution-please/52501 "2006-01-15T12:19:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joergd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@joergd](https://www.ruby-forum.com/u/joergd)\
**Post date:** [January 15, 2006, 12:19pm UTC](https://www.ruby-forum.com/t/saving-one-many-associations-elegant-solution-please/52501/1 "2006-01-15T12:19:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joergd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@joergd](https://www.ruby-forum.com/u/joergd)\
**Post date:** [January 15, 2006, 1:07pm UTC](https://www.ruby-forum.com/t/saving-one-many-associations-elegant-solution-please/52501/2 "2006-01-15T13:07:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joergd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@joergd](https://www.ruby-forum.com/u/joergd)\
**Post date:** [January 15, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/saving-one-many-associations-elegant-solution-please/52501/3 "2006-01-15T15:32:15Z")

</div>

Ah of course. I always misread the Agile Rails book for some reason.  
Thanks.

---

<div class="post-metadata">

**Author:** ![joergd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@joergd](https://www.ruby-forum.com/u/joergd)\
**Post date:** [January 15, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/saving-one-many-associations-elegant-solution-please/52501/4 "2006-01-15T20:17:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joergd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@joergd](https://www.ruby-forum.com/u/joergd)\
**Post date:** [January 15, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/saving-one-many-associations-elegant-solution-please/52501/5 "2006-01-15T20:24:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joergd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@joergd](https://www.ruby-forum.com/u/joergd)\
**Post date:** [January 16, 2006, 7:22am UTC](https://www.ruby-forum.com/t/saving-one-many-associations-elegant-solution-please/52501/6 "2006-01-16T07:22:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joergd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@joergd](https://www.ruby-forum.com/u/joergd)\
**Post date:** [February 3, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/saving-one-many-associations-elegant-solution-please/52501/7 "2006-02-03T23:21:45Z")

</div>

Is there a similar way to do this with HABTM relationships? I don’t  
think build is included.
