# HABTM Checkboxes and Multiple Records headache

**URL:** <https://www.ruby-forum.com/t/habtm-checkboxes-and-multiple-records-headache/166210>\
**Category:** Rails\
**Created:** [April 22, 2009, 7:38pm UTC](https://www.ruby-forum.com/t/habtm-checkboxes-and-multiple-records-headache/166210 "2009-04-22T19:38:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![infinteVerve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e19adc/32.png) [@infinteVerve](https://www.ruby-forum.com/u/infinteVerve)\
**Post date:** [April 22, 2009, 7:38pm UTC](https://www.ruby-forum.com/t/habtm-checkboxes-and-multiple-records-headache/166210/1 "2009-04-22T19:38:14Z")

</div>

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]  
[]”, instrument.id, production\_edit.instruments.include?(instrument)  
%\> \<%= instrument.name %\>  
  
\<% 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|  
production\_edits.build(attributes)

```
end

```

end  
…

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”]},  
{“instrument\_ids”=\>[“2”]}]

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.
