Benjamin Meichsner
2007-Jan-12 11:01 UTC
how to update/create a collection of two models in one form
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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]. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Benjamin Meichsner
2007-Jan-13 01:26 UTC
Re: how to update/create a collection of two models in one f
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---