alex.bour
alex.bour

Reputation: 2976

Rails, fields_for, merging records

I'm using fields_for(), not for a nested form, but to display a form containing joined items. I need to display data from @seasons, that help me filling drinks...

<% @seasons do |season| %>  
      
   <%= fields_for "drinks[]", season.drink do |f| %>

      ...
      <%= f.select :optimized_region_id ... %>
      ...

   <% end %>
<% end %>

It works well. However, even if @seasons are never the same, some season.drink could be the sames items as they are "parent" relations. (same season.drink.id) It's not a problem for me.

My issue is that the form sends this:

drinks"=>{
"e80e15c1-a5d4-4df4-80c6-2efa96e39793"=>{"optimized_status"=>"1", "optimized_nickname"=>"Alex"}, 
"b7501fe0-3a78-412e-88d5-e7643d761a98"=>{"optimized_status"=>"1", "optimized_nickname"=>"Paul"}
...
}

and should send this:

drinks"=>{
"e80e15c1-a5d4-4df4-80c6-2efa96e39793"=>{"optimized_status"=>"1", "optimized_nickname"=>"Alex"}, 
"e80e15c1-a5d4-4df4-80c6-2efa96e39793"=>{"optimized_status"=>"0", "optimized_nickname"=>"Alex"}, 
"b7501fe0-3a78-412e-88d5-e7643d761a98"=>{"optimized_status"=>"1", "optimized_nickname"=>"Paul"}
...
}

It seems that Rails is merging drinks that have the same id when the form is sent. Is there a possibility to avoid this and send all drinks even if they have the same drink.id ?

Upvotes: 1

Views: 26

Answers (1)

alex.bour
alex.bour

Reputation: 2976

I found a workaround setting the season_id in the [] :

<%= fields_for "drinks[#{season.id}]", record.drink do |f| %>

Now, all my drinks are sent from the form, evene the ones with same id.

Upvotes: 0

Related Questions