Forum: Ruby on Rails how to update/create a collection of two models in one form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Benjamin M. (Guest)
on 2007-01-12 13:01
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 %>
 <tr class="<%= cycle('even', 'odd') %>">
  <td>
    <%= text_field( 'article[]', 'name')%></td>

  <td>
    <%= text_field 'article[]', 'unit'%></label></td>

  <td>
    <%= text_field_tag "article_price[#{@article.id}][price]",
    (@price.price if @price) %></td>

  <td>
    <%= text_field_tag "article_price[#{@article.id}][unit_quantity]",
    (@price.unit_quantity if @price) %></td>

  <td>
    <%= text_field_tag "article_price[#{@article.id}][order_number]",
   (@price.order_number if @price) %></td>
[.......]
      <% 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
mayoz (Guest)
on 2007-01-12 22:05
> 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].
Benjamin M. (Guest)
on 2007-01-13 03:26
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
This topic is locked and can not be replied to.