# How to update/create a collection of two models in one form

**URL:** <https://www.ruby-forum.com/t/how-to-update-create-a-collection-of-two-models-in-one-form/81744>\
**Category:** Rails\
**Created:** [January 12, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/how-to-update-create-a-collection-of-two-models-in-one-form/81744 "2007-01-12T12:01:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![benni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@benni](https://www.ruby-forum.com/u/benni)\
**Post date:** [January 12, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/how-to-update-create-a-collection-of-two-models-in-one-form/81744/1 "2007-01-12T12:01:35Z")

</div>

hello

I would like to know, how to create oder update two models in one form.  
and especially i think of a collection of models.

e.g. i have the model article and its related model article\_price. the  
edit-view is a table with an article and its current price in each row.  
here’s an exception:

\<%= start\_form\_tag :action =\> ‘updateAllArticles’%\>  
[…]  
\<% for @article in @articles %\>  
\<% @price = @article.current\_price %\>

 \<%= text\_field( 'article[]', 'name')%\> \<%= text\_field 'article[]', 'unit'%\> \<%= text\_field\_tag "article\_price[#{@article.id}][price]", (@price.price if @price) %\> \<%= text\_field\_tag "article\_price[#{@article.id}][unit\_quantity]", (@price.unit\_quantity if @price) %\> \<%= text\_field\_tag "article\_price[#{@article.id}][order\_number]", (@price.order\_number if @price) %\> [.......] \<% end %\> the current price will be send with the id of his article, so i later i can identify, where the price belongs to. when the price is changed, i will not update it, but create a new one.

when i update the articles i use the described method:  
update(params[:item].keys, params[:item].values)

but how do i have access to the articles. as i said, i have to check, if  
the article\_price is changed an the maybe create a new one…

I hope you can help me.

greetings  
benni

---

<div class="post-metadata">

**Author:** ![benni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@benni](https://www.ruby-forum.com/u/benni)\
**Post date:** [January 12, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/how-to-update-create-a-collection-of-two-models-in-one-form/81744/2 "2007-01-12T21:05:28Z")

</div>

> the current price will be send with the id of his article, so i later i  
> can identify, where the price belongs to. when the price is changed, i  
> will not update it, but create a new one.
> 
> when i update the articles i use the described method:  
> update(params[:item].keys, params[:item].values)
> 
> but how do i have access to the articles. as i said, i have to check, if  
> the article\_price is changed an the maybe create a new one…

I don’t understand your question completely but I’ll try to help even  
though I’m new to ruby on rails myself. You said in your story that  
you’re sending the article and price(“the current price will be send  
with the id of his article”).  
I would say in the action updateAllArticles you can do something like

@article = Article.find(:all, [:conditions =\> “id =?”,params[:id]]

and then check if the price of the found @article correspond to the  
params[:price].

---

<div class="post-metadata">

**Author:** ![benni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@benni](https://www.ruby-forum.com/u/benni)\
**Post date:** [January 13, 2007, 2:26am UTC](https://www.ruby-forum.com/t/how-to-update-create-a-collection-of-two-models-in-one-form/81744/3 "2007-01-13T02:26:04Z")

</div>

mayoz wrote:

> I don’t understand your question completely but I’ll try to help even  
> though I’m new to ruby on rails myself. You said in your story that  
> you’re sending the article and price(“the current price will be send  
> with the id of his article”).  
> I would say in the action updateAllArticles you can do something like
> 
> @article = Article.find(:all, [:conditions =\> “id =?”,params[:id]]
> 
> and then check if the price of the found @article correspond to the  
> params[:price].

hey mayoz

sorry, I made a mistake above. My question was, how can I access the  
article\_price, not the article, in my controller. I thought about a  
method like such:  
params[:article\_price].each do |price|  
new\_price = ArticlePrice.new[price.values]…

but thats not working.

thanks,  
benni
