Home > OS >  How to loop through nested array in rails
How to loop through nested array in rails

Time:04-05

Currently I exploring Ruby on Rails. So I try to save nested array in database but I encountering some problem. The problem is I can take the value from the input boxes but not sure how to loop the data correctly.

The value in my parameter is

#<ActionController::Parameters {"authenticity_token"=>"HrR9-qX2HZrG3Q71smHdcO4s8ke8SwoUzhAZGdTOuVsuAq-RQO7Rp9YIX6j988uj9PTkA-pffvM2IBlKGi0C2w", "supplier"=>#<ActionController::Parameters {"company_id"=>"1", "first_name"=>"aa", "last_name"=>"aa", "username"=>"aa", "email"=>"[email protected]", "phone_number"=>"65738290"} permitted: false>, "supplier_item"=>{"item_ids"=>["2", "4"], "location_ids"=>["1", "2"], "supplier_prices"=>["7.8", "45.0"]}, "item_select"=>"1", "location_select"=>"1", "commit"=>"Create Supplier", "controller"=>"suppliers", "action"=>"create"} permitted: false>

What I'm trying to do is I want to loop params[:supplier_item]. Which is the data inside here.

#<ActionController::Parameters {"item_ids"=>["2", "4"], "location_ids"=>["1", "2"], "supplier_prices"=>["7.8", "45.0"]} permitted: false>

So I want to loop the params[:supplier_item] and save into join table called supplier_item. So I try few ways to loop thru the data but I still having some issue.

Do you guys have any opinion or idea how to do this please so share. I attach my code down below. Thank you

 @supplier = Supplier.new(supplier_params)
      if @supplier.save
        params[:supplier_item][:item_ids].each do |item_id|
          supplier_prices = params[:supplier_item][:supplier_prices]
          location_ids = params[:supplier_item][:location_ids]
          supplier_item = SupplierItem.new(item_id: item_id[], supplier_id: @supplier.id, location_id: location_ids[], supplier_price: supplier_prices )
          supplier_item.save
        end
        flash[:success] = "New supplier created"
        redirect_to supplier_path(@supplier)
      else
        render 'new'
      end

CodePudding user response:

I assume that SupplierItem object attributes should be taken from item_ids, location_ids and supplier_prices arrays by the same index.

The code:

@supplier = Supplier.new(supplier_params)
if @supplier.save
  item_params = params.require(:supplier_item).permit(item_ids: [], location_ids: [], supplier_prices: [])
  # => {"item_ids"=>["2", "4"], "location_ids"=>["1", "2"], "supplier_prices"=>["7.8", "45.0"]}
  items = item_params[:item_ids].zip(*item_params.values_at(:location_ids, :supplier_prices))
  # => [["2", "1", "7.8"], ["4", "2", "45.0"]]
  items.map do |item_id, location_id, supplier_price|
    supplier_item = SupplierItem.new({
      item_id:        item_id,
      location_id:    location_id,
      supplier_price: supplier_price,
      supplier_id:    @supplier.id,
    }.compact)
    supplier_item.save # TODO: check if it saved
  end
  flash[:success] = "New supplier created"
  redirect_to supplier_path(@supplier)
else
  render 'new'
end

PS: Don't use float/double for money.

  • Related