Carlos Morales
Carlos Morales

Reputation: 1149

How can save array values? (multiple inserts)

How can I do multiple insert after saving the 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 is the 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 is the view:

<% form_for :obj_flow, :url => {:controller=>"flow_budget",:action=>'create_flow'} do |f|%>
  <%= f.text_field :proyected_money %>  

  <% @analysis_expenses.each do |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 do |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 is my 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 INTO `flow_budgets` (`proyected_money` ) VALUES(8000)

I want to save

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

Please somebody can help me?

Upvotes: 0

Views: 42

Answers (1)

Albin
Albin

Reputation: 3012

I think what you are looking for is accepts_nested_attributes_for :category_expense_budgets

I presume your model ObjFlow has has_many :category_expense_budgets.

Then it should work if you add the acceptance of nested attributes and format your form so the params hash gets the right format. The easiest way to accomplish this i think is something like:

<% form_for @obj_flow, :url => {:controller=>"flow_budget",:action=>'create_flow'} do |f|%>
  <%= f.text_field :proyected_money %>
  <% @analysis_expenses.each do |analysis_expense| %>

     <% f.fields_for analysis_expense do |nested_f| %> # THIS IS A NEW IMPORTANT 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 do |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 you would like: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Upvotes: 1

Related Questions