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


#1

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


#2

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].


#3

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