Monday 15 July 2013

ruby on rails - How can save array values? (multiple inserts) -



ruby on rails - How can save array values? (multiple inserts) -

how can multiple insert after saving main issue?

tables:

flow_budgets |id| |proyected_money| 1 5000 category_expense_budgets |id| |amount| |flow_budget_id| |category_expense_id| 1 1000 1 1 2 2000 1 1 3 3000 1 1 4 4000 1 2 5 5000 1 2 6 6000 1 2 category_expenses |id| |name| |analysis_expense_id| 1 category 1 1 2 category 2 1 3 category 3 2 4 category 4 2 analysis_expenses |id| |name| 1 analysis 1 2 analysis 2

here controller:

def new_flow @analysis_expenses = analysisexpense.all @obj_flow = flowbudget.new(params[:obj_flow]) end def create_flow obj_flow = flowbudget.new(params[:obj_flow]) obj_flow.save() if obj_flow.save() @flow_budget_id = flowbudget.last.id obj_proyected = categoryexpensebudget.new end end

here view:

<% form_for :obj_flow, :url => {:controller=>"flow_budget",:action=>'create_flow'} |f|%> <%= f.text_field :proyected_money %> <% @analysis_expenses.each |analysis_expense| %> <label><%= analysis_expense.name %></label> <%= text_field_tag "proyected_analysis_expenses",{},:name => "proyected_analysis_expense[amount][]", :id => "proyected_analysis_expense_#{analysis_expense.id}" %> <table> <% analysis_expense.category_expenses.each |category_expense|%> <tr> <td><%= category_expense.name %>:</td> <td><%= text_field_tag "proyected_category_expenses",{},:name => "proyected_category_expense[name][]", :id => "proyected_category_expense_#{category_expense.id}" %></td> </tr> <% end %> </table> <% end %> <% end %>

here log:

processing flowbudgetcontroller#create_flow (for 127.0.0.1) [post] parameters: {"proyected_money"=>"8000", "proyected_category_expense"=>{"amount"=>["2100", "2500" ], "proyected_analysis_expense"=>{"amount"=>["1000", "1100", "1200", "1300" ]} insert `flow_budgets` (`proyected_money` ) values(8000)

i want save

insert `flow_budgets` (`proyected_money` ) values(8000) insert `category_expense_budgets` (`amount`,'flow_budget_id','category_expense_id' ) values(1000,1,1) insert `category_expense_budgets` (`amount`,'flow_budget_id','category_expense_id' ) values(1100,1,2) insert `category_expense_budgets` (`amount`,'flow_budget_id','category_expense_id' ) values(1200,1,3) insert `category_expense_budgets` (`amount`,'flow_budget_id','category_expense_id' ) values(1300,1,4)

please can help me?

i think looking accepts_nested_attributes_for :category_expense_budgets

i presume model objflow has has_many :category_expense_budgets.

then should work if add together acceptance of nested attributes , format form params hash gets right format. easiest way accomplish think like:

<% form_for @obj_flow, :url => {:controller=>"flow_budget",:action=>'create_flow'} |f|%> <%= f.text_field :proyected_money %> <% @analysis_expenses.each |analysis_expense| %> <% f.fields_for analysis_expense |nested_f| %> # new of import line <label><%= analysis_expense.name %></label> <%= nested_f.text_field "proyected_analysis_expenses",{},:name => "proyected_analysis_expense[amount][#{analysis_expense.id}]", :id => "proyected_analysis_expense_#{analysis_expense.id}" %> <table> <% analysis_expense.category_expenses.each |category_expense|%> <tr> <td><%= category_expense.name %>:</td> <td><%= text_field_tag "proyected_category_expenses",{},:name => "proyected_category_expense[name][#{analysis_expense.id}]", :id => "proyected_category_expense_#{category_expense.id}" %></td> </tr> <% end %> </table> <% end %> <% end %> <% end %>

read more here if like: http://api.rubyonrails.org/classes/activerecord/nestedattributes/classmethods.html

ruby-on-rails ruby

No comments:

Post a Comment