Forum: Ruby on Rails HABTM Checkboxes and Multiple Records headache...

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
E82430bd8e805ef262f84382326ab877?d=identicon&s=25 infinteVerve (Guest)
on 2009-04-22 19:38
(Received via mailing list)
I haven't been able to get this to work for some reason, and its
driving me nuts, and help is greatly appreciated.
So here's the deal:

I'm have a table called Songs
Each song has_many versions called Production_edits
each production_edit has its own instrumentation.
Instruments has_and_belongs_to_many Production_edits and vice versa.
and i have my instruments_production_edits join table

In my new song form, you can add multiple production_edits, the only
problem is that since I'm using HABTM checkboxes for the
instrumentation, for every instrument i check, rails interprets it as
a seperate production_edit.

Here's my view in for the new song form:

<% form_for(@song, :url => {:action => 'create_song'}, :html =>
{ :multipart => true } ) do |f|%>
<%= error_messages_for :song %>...

(below is in my _production_edit partial)
<% @production_edit = production_edit %>
<% fields_for "song[production_edit_attributes][]", production_edit do
|production_edit_form| %>
<%= error_messages_for :production_edit %>

<%= production_edit_form.text_field :title %> (etc...)
<% for instrument in Instrument.find(:all) %>
<%= check_box_tag "song[production_edit_attributes][][instrument_ids]
[]",, production_edit.instruments.include?(instrument)
%> <%= %><br/>
<% end %>
<% end %>

Then in my song.rb model i have:
validates_associated :production_edits

  def production_edit_attributes=(production_edit_attributes)
    production_edit_attributes.each do |attributes|


When i fill out the form with each production_edit having more than
one instrument i get something like this in my parameters:

"production_edit_attributes"=>[{"title"=>"", "instrument_ids"=>[]},
{"title"=>"", "instrument_ids"=>[]}, {"instrument_ids"=>["1"]},
{"instrument_ids"=>["3"]}, {"instrument_ids"=>["1"]},

when I'm actually trying to achieve this:

"production_edit_attributes"=>[{"title"=>"", "instrument_ids"=>
["1,"3"]}, {"title"=>"", "instrument_ids"=>["1","2"]}]

I've also tried something similar with a multiple select box, however
I get the same result in the params.  Any ideas?  or let me know if
I'm not clear enough on what I'm trying to do.
This topic is locked and can not be replied to.