# Associated models HABTM form problems

**URL:** <https://www.ruby-forum.com/t/associated-models-habtm-form-problems/203798>\
**Category:** Rails\
**Created:** [February 27, 2011, 6:12pm UTC](https://www.ruby-forum.com/t/associated-models-habtm-form-problems/203798 "2011-02-27T18:12:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 27, 2011, 6:12pm UTC](https://www.ruby-forum.com/t/associated-models-habtm-form-problems/203798/1 "2011-02-27T18:12:52Z")

</div>

I’m building an app for displaying events information.

The app has a database of bands, and events

an event can have many bands, and a band can have many events, so I’ve  
gone for a has\_and\_belongs\_to\_many association between the two models,  
with the join table bands\_events.

In order to be able to dynamically add bands, I’ve refactored the ‘add a  
band’ part of the form into a partial called \_band, this form has 2  
parts, a text\_field to enter the name of a new band, or a  
collection\_select drop down select box to select a band already stored.  
The idea is that you can make a new band on the fly by typing into the  
text\_field, or you select an existing one from the menu.

The text field is working fine, and creates new associated bands, but  
the collection\_select selection isn’t getting passed through correctly,  
and I’m very confused…

(I got the code from R Bates’s railscast on complex forms pt 2)

my controller initialises the band into memory  
EventsController  
def new  
@event = Event.new  
@bands = Band.order(’“band\_name” ASC ')  
1.times {@event.bands.build}  
end

The views are currently very simple ( in haml if you’re thinking it  
looks weird!)

---------------------------- new.html.haml  
%h1 Create a new event

=form\_for [:events\_admin, @event], :url =\> events\_admin\_events\_path,  
:html =\> { :multipart =\> true } do |f|  
=render :partial=\>“events\_form”, :locals=\>{:event =\> @event, :f=\>f}

---------------- \_events\_form.html.haml

%fieldset  
.half  
=f.label “Event Name”  
=f.text\_field :event\_name

```
=render :partial => "band", :collection => @event.bands

=f.submit "Save Event"

```

----------------- \_band.html.haml

=fields\_for “event[band\_attributes][]”, band do |band\_form|  
=band\_form.label “Add a new band”  
=band\_form.text\_field :new\_band\_name

// This is not working at all, for some reason the params are not  
being passed through  
=band\_form.label “Select from saved Bands”  
=collection\_select(band, :saved\_band\_name, @bands, :band\_name,  
:band\_name, {:selected =\> @selected\_band})

when the submit button is pressed, the method band\_attributes= is called  
in the Event model class, and that looks like this:

event.rb - class Event \< ActiveRecord::Base

def band\_attributes=(band\_attributes)

```
band_attributes.each do |attributes|
  new_band_name = attributes[:new_band_name]

  logger.debug" ______NEW BAND NAME FROM PARAMS_______"
  logger.debug(new_band_name)

  if new_band_name.empty?
    saved_band_name = attributes[:saved_band_name]
    band_object = Band.where(:band_name => saved_band_name)[0]
    logger.debug" ______ BAND NAME FROM PARAMS DROP DOWN LIST

```

\_\_\_\_\_\_\_"  
logger.debug(saved\_band\_name)

```
  else
    attributes[:band_name] = new_band_name
    bands.build(attributes)
  end

end

```

end # of band\_attributes

Typing into the text\_field works fine, the new\_band\_name gets passed  
through, and saved, but the saved\_band\_name from the collection select  
drop down list does not get passed through in the band\_attributes.

From looking at the logs, it looks like the ‘saved\_band\_name’ parameter  
is not getting grouped with the other band data

LOGS:

Parameters: {“commit”=\>“Save Event”,  
“authenticity\_token”=\>“bRJcMHn+PL3pwpeVcgyUpd/IgRjNiGih2cfadZoPJSk=”,  
“utf8”=\>“✓”, “#Band:0x1049e4008”=\>{“saved\_band\_name”=\>“A BAND I’VE  
SAVED”}, “event”=\>{“band\_attributes”=\>[{“new\_band\_name”=\>""}],  
“start\_time(1i)”=\>“2011”, “music\_sample\_link”=\>""………

Does anyone know what is going on here, and how I can get the parameter  
from the collection\_select box to be passed in the same hash as the  
new\_band\_name??

All help appreciated massively.

Mike

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [February 27, 2011, 9:47pm UTC](https://www.ruby-forum.com/t/associated-models-habtm-form-problems/203798/2 "2011-02-27T21:47:53Z")

</div>

On Feb 27, 5:12pm, Michael B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The text field is working fine, and creates new associated bands, but  
> the collection\_select selection isn’t getting passed through correctly,  
> and I’m very confused…
> 
> (I got the code from R Bates’s railscast on complex forms pt 2)

Not answering your question directly, but these days you probably want  
to use the accepts\_nested\_attributes stuff that is built into rails  
2.3 and higher.

> =band\_form.label “Select from saved Bands”  
> =collection\_select(band, :saved\_band\_name, @bands, :band\_name,  
> :band\_name, {:selected =\> @selected\_band})

That first argument to collection\_select should be the name of the  
attribute, ie a symbol or string rather than an actual instance of  
band. The easy way would be to use the form builder, ie  
band\_form.collection\_form :saved\_band\_name, … just like you’re doing  
with other form inputs

Fred

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [February 28, 2011, 4:27pm UTC](https://www.ruby-forum.com/t/associated-models-habtm-form-problems/203798/3 "2011-02-28T16:27:57Z")

</div>

Thanks for your help!

> =band\_form.label “Select from saved Bands”  
> =collection\_select(band, :saved\_band\_name, @bands, :band\_name,  
> :band\_name, {:selected =\> @selected\_band})

That first argument to collection\_select should be the name of the  
attribute, ie a symbol or string rather than an actual instance of  
band. The easy way would be to use the form builder, ie  
band\_form.collection\_form :saved\_band\_name, … just like you’re doing  
with other form inputs.

* * *

I really didn’t know I could use collection select in this way, thanks  
for the tip, not quite sure how it’s working, but

band\_form.collection\_select :saved\_band\_name, @bands, :band\_name,  
:band\_name, {:selected =\> @selected\_band}

seems to be passing the selected band name through exactly as it should  
be :-))

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [February 28, 2011, 4:47pm UTC](https://www.ruby-forum.com/t/associated-models-habtm-form-problems/203798/4 "2011-02-28T16:47:00Z")

</div>

On Feb 28, 3:27pm, Michael B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> with other form inputs.
> 
> * * *
> 
> I really didn’t know I could use collection select in this way, thanks  
> for the tip, not quite sure how it’s working, but
> 
> band\_form.collection\_select :saved\_band\_name, @bands, :band\_name,  
> :band\_name, {:selected =\> @selected\_band}

It’s exactly the same as band\_form.text\_field. The form builder object  
(ie band\_form) encapsulates the object being edited & info about the  
appropriate prefix for the input names.

Fred
