Forms with nested attributes

Hi all,

A bit stuck with a complex form.

I’ve 2 models, events and bands.
bands have many events, and events have many bands, so…
they’re related with a has_and_belongs_to_many, with the appropriate
joining table.

In my new events form I’ve got some jquery to add new bands dynamically.

in the _band form that’s generated, I have a collection_select drop down
menu that has a list of all the bands

//controller
@bands = Band.order('“band_name” ASC ')

// form
ff.collection_select :band_name, @bands, :band_name, :band_name

When I select a band from the list the event saves fine, has the
correct band or bands associated, but for each band it makes a new band
instance as well. I want it to just associate the band with the event.

currently:
If I have 3 bands saved, beatles, stones and eagles
I make a new event and select ‘beatles’, from the drop down then save,
a new event is saved, and associated with a band called beatles
BUT, there are now 2 bands called beatles in the database.
I want it to be associated with the original beatles

I’ve tried using
ff.collection_select :id, @bands, :id, :band_name

passing in the id instead as indicated in the nested_attributes docs
(ActiveRecord::NestedAttributes::ClassMethods)

but this returns a 404 error

I’m getting this in the logs:

Band Load (0.3ms) SELECT “bands”.* FROM “bands” INNER JOIN
“bands_events” ON “bands”.id = “bands_events”.band_id WHERE
(“bands_events”.event_id = NULL ) AND (“bands”.“id” IN (1))
Completed in 63ms
Rendered layouts/_header.html.haml (13.3ms)
Rendered site/404.html.haml within layouts/error (169.8ms)

it looks like it’s getting an id for bands, but the id for the event is
NULL, any ideas how to get this going???

any help appreciated :slight_smile:

if you wanna see other bits of code also let me know, I didn’t want the
post getting too long

Mike

On Mar 18, 2:20pm, Michael B. [email protected] wrote:

Completed in 63ms
Rendered layouts/_header.html.haml (13.3ms)
Rendered site/404.html.haml within layouts/error (169.8ms)

it looks like it’s getting an id for bands, but the id for the event is
NULL, any ideas how to get this going???

What are the actual parameters that are submitted ?

Fred

Hi Fred,

Thanks for helping out with this, sorry bout slow reply, I haven’t been
able to get back to this, and for some reason email notifications didn’t
happen, anyway…

These are the paramaters when I try to submit using
ff.collection_select :id, @bands, :id, :band_name

Started POST “/admin/events/events/2” for 127.0.0.1 at Mon Mar 21
09:22:12 +0000 2011
Processing by Admin::Events::EventsController#update as HTML
Parameters: {“commit”=>“Save this event”,
“authenticity_token”=>“x9LwG7hmAp4MruUJXpR3DzTxABOFEnmnEfpKYw10yDQ=”,
“utf8”=>“✓”, “id”=>“2”, “event”=>{“image_id”=>“465”,
“music_sample_link”=>"", “bands_attributes”=>{“0”=>{“new_band_name”=>"",
“id”=>“2”, “_destroy”=>“false”}}, “new_venue_name”=>"", “venue_id”=>“1”,
“energy_level”=>“56”, “is_published”=>“0”,
“event_name”=>“cabaretretret”, “special_offers”=>"", “genre”=>"",
“is_all_event”=>“0”, “is_ten_event”=>“1”, “description”=>“caberet”,
“start_time”=>“2000-01-01 08:40”, “buy_tickets_url”=>""}}

If you look at the “bands_attributes” hash, “0” is used to determine
which band this is, with each new band on the form this increases by 1,
“new_band_name” is a virtual attribute that will be used in the future
to create bands on the fly, and “id” =>“2” is the correct id I want to
associate with the event.

If there was a direct relationship between bands and events I think this
would be simpler, but because it’s a HABTM relationship, I’m unsure what
to do.

Thanks for your help!

Mike

Yeah I might give that a try, thanks for the help

M

On Mar 21, 9:32am, Michael B. [email protected] wrote:

Might it be easier to use a has many through?

Fred