# Updateform with a has\_many relationship?

**URL:** <https://www.ruby-forum.com/t/updateform-with-a-has-many-relationship/57805>\
**Category:** Rails\
**Created:** [April 14, 2006, 11:26am UTC](https://www.ruby-forum.com/t/updateform-with-a-has-many-relationship/57805 "2006-04-14T11:26:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gerard\_de\_Brieder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/eb8c5e/32.png) [@Gerard\_de\_Brieder](https://www.ruby-forum.com/u/Gerard_de_Brieder)\
**Post date:** [April 14, 2006, 11:26am UTC](https://www.ruby-forum.com/t/updateform-with-a-has-many-relationship/57805/1 "2006-04-14T11:26:46Z")

</div>

Hello all,

I would like to be able to update the “amount” of a certain item in  
me order list.  
The problem I run into is how to define the text\_field element.

@order is the current order, which has\_many :order\_item which in turn  
belongs\_to :order.

order\_item has a field “amount” which should be modified…

What I tried is this below, but I get a undefined method  
`order\_item.amount’ for #Order:0x2375764

\<%total\_price=0%\>  
\<removed\_email\_address@domain.invalid\_item.each{|item|%\>  
\<tr class=\<%=cycle(‘even’,‘odd’)%\>\>  
\<%=item.product.name%\>  
\<%=text\_field ‘order’,‘order\_item.amount’,:size=\>3%\>  
\<%=item.product.price%\>  
\<%=(item.product.price_item.amount)%\>  
\<%=image\_tag ‘delete’%\>  
\<%total\_price+=(item.product.price_item.amount)%\>

\<%}%\>

With kind regards,

Gerard de Brieder.  
Govannon.  
web : [http://www.govannon.nl](http://www.govannon.nl)  
email : removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Gerard\_de\_Brieder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/eb8c5e/32.png) [@Gerard\_de\_Brieder](https://www.ruby-forum.com/u/Gerard_de_Brieder)\
**Post date:** [April 17, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/updateform-with-a-has-many-relationship/57805/2 "2006-04-17T20:54:24Z")

</div>

Hi all,

This is certainly a basic one, but I have the same question.

Mickael.

On 4/14/06, Gerard de Brieder [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> order\_item has a field “amount” which should be modified…
> 
> \<%=(item.product.price\*item.amount)%\> \<%=image\_tag 'delete'%\> \<%total\_price+=(item.product.price\*item.amount)%\> \<%}%\>

---

<div class="post-metadata">

**Author:** ![Gerard\_de\_Brieder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/eb8c5e/32.png) [@Gerard\_de\_Brieder](https://www.ruby-forum.com/u/Gerard_de_Brieder)\
**Post date:** [April 17, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/updateform-with-a-has-many-relationship/57805/3 "2006-04-17T22:13:50Z")

</div>

Gerard de Brieder wrote:

> Hello all,
> 
> I would like to be able to update the “amount” of a certain item in  
> me order list.  
> The problem I run into is how to define the text\_field element.
> 
> @order is the current order, which has\_many :order\_item which in turn  
> belongs\_to :order.
> 
> order\_item has a field “amount” which should be modified…
> 
> What I tried is this below, but I get a undefined method  
> `order\_item.amount’ for #Order:0x2375764
> 
> \<%total\_price=0%\>  
> \<removed\_email\_address@domain.invalid\_item.each{|item|%\>
> 
> \> \<%=item.product.name%\> \<%=text\_field 'order','order\_item.amount',:size=\>3%\> \<%=item.product.price%\> \<%=(item.product.price\*item.amount)%\> \<%=image\_tag 'delete'%\> \<%total\_price+=(item.product.price\*item.amount)%\> \<%}%\>
> 
> With kind regards,
> 
> Gerard de Brieder.  
> Govannon.  
> web : [http://www.govannon.nl](http://www.govannon.nl)  
> email : removed\_email\_address@domain.invalid

Are you trying to save the total amount of the order to the order record  
when you submit? If so, then, you should do this:

\<% order.amount = 0 %\>  
\<%= start\_form\_tag … %\>  
\<% for order\_item in @order.order\_items %\>  
\<% your display logic here %\>  
\<% order.amount += order\_item.item.price \* order\_item.amount %\>  
\<% end %\>  
\<% hidden\_field :order, :amount %\>  
\<%= end\_form\_tag %\>

This is untested, but I think it should work. Let me know how it works  
out.

---

<div class="post-metadata">

**Author:** ![Gerard\_de\_Brieder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/eb8c5e/32.png) [@Gerard\_de\_Brieder](https://www.ruby-forum.com/u/Gerard_de_Brieder)\
**Post date:** [April 17, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/updateform-with-a-has-many-relationship/57805/4 "2006-04-17T22:22:03Z")

</div>

Hi Bryan,

I think I can reply for Gerard.  
The problem really is in the ‘display logic’ you’ve summarized.

He gets a undefined method  
`order\_item.amount’ for #Order:0x2375764

here:

\<%=item.product.name%\>

Mickael.
