# How can I create OrderItems that go in a Order?

**URL:** <https://www.ruby-forum.com/t/how-can-i-create-orderitems-that-go-in-a-order/99299>\
**Category:** Rails\
**Created:** [July 21, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/how-can-i-create-orderitems-that-go-in-a-order/99299 "2006-07-21T16:16:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [July 21, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/how-can-i-create-orderitems-that-go-in-a-order/99299/1 "2006-07-21T16:16:25Z")

</div>

I suspect my problem is similar to having an order and wanting to have  
order items as part of the order, so I’ll explain the problem in those  
more familiar terms and then explain what if anything makes my actual  
situation different at the end.

In an Order lets say you have: shipping\_address and a shipped bool.  
OrderItem has quantity and name

So Order has\_many :OrderItems, and OrderItem belongs\_to :Order

It’s pretty easy to make it so you can create an Order then go to  
another URL and create a OrderItem and associate (say with a select)  
the Order the OrderItem belongs to, and so on.

What I really want though is the ability to have users click to create  
an Order, fill out those fields, then on the same page fill out the  
details for the first OrderItem and be able to click say a + or a ‘Add  
another’ link or something and add another OrderItem and so on.

This is so common there must be a tutorial or example somewhere that  
someone can point me at so I can RTFM? I have (I think) all the  
Pragmatic Programmers Rails books if there is a section in there I’ve  
missed.

I was also wondering how to make it so that you can’t have an Order in  
existence that has no OrderItems, in the sense that I don’t want  
Orders floating around that makes no sense. I can think of ways maybe  
of doing this - but not with everything on one page.

The actual problem I have is I think quite similar. I am implementing  
a ‘Requisition’ system at work - where a requisition is like an order  
(it has a supplier, user who requested it, user who approved it etc),  
and contains a list of items that the user wants requisitioned.

Thanks!  
-Rob

## –

“On two occasions, I have been asked [by members of Parliament],  
‘Pray, Mr. Babbage, if you put into the machine wrong figures, will  
the right answers come out?’ I am not able to rightly apprehend the  
kind of confusion of ideas that could provoke such a question.”  
– Charles Babbage (1791-1871)

“98.5% of DNA is considered to be junk DNA with no known purpose.  
Maybe it’s XML tags.” – Anon

[http://www.robhulme.com/](http://www.robhulme.com/)  
[http://robhu.livejournal.com/](http://robhu.livejournal.com/)

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [July 22, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/how-can-i-create-orderitems-that-go-in-a-order/99299/2 "2006-07-22T22:31:24Z")

</div>

> This is so common there must be a tutorial or example somewhere that  
> someone can point me at so I can RTFM? I have (I think) all the  
> Pragmatic Programmers Rails books if there is a section in there I’ve  
> missed.  
> Is this not possible in Rails?

## –

“On two occasions, I have been asked [by members of Parliament],  
‘Pray, Mr. Babbage, if you put into the machine wrong figures, will  
the right answers come out?’ I am not able to rightly apprehend the  
kind of confusion of ideas that could provoke such a question.”  
– Charles Babbage (1791-1871)

“98.5% of DNA is considered to be junk DNA with no known purpose.  
Maybe it’s XML tags.” – Anon

[http://www.robhulme.com/](http://www.robhulme.com/)

> **[robhu](https://robhu.livejournal.com/)**
>
> robhu - the new blog in LiveJournal. There should be new interesting records soon.

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [July 23, 2006, 2:42am UTC](https://www.ruby-forum.com/t/how-can-i-create-orderitems-that-go-in-a-order/99299/3 "2006-07-23T02:42:02Z")

</div>

@new\_order\_item = Order.orderitem.create( :quantity =\> ‘20’ )

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [July 23, 2006, 2:55am UTC](https://www.ruby-forum.com/t/how-can-i-create-orderitems-that-go-in-a-order/99299/4 "2006-07-23T02:55:42Z")

</div>

Jan wrote:

> @new\_order\_item = Order.orderitem.create( :quantity =\> ‘20’ )

oops

@new\_order\_item = Order.find( params[:id] ).orderitems.create( :quantity  
=\> ‘20’, :name =\> ‘example’ )
