views:

31

answers:

2

hi, i have a event model that has_and_belongs_to_many artists

class Event < ActiveRecord::Base
 has_and_belongs_to_many :humans, :foreign_key => 'event_id', :association_foreign_key => 'human_id'
end

in the form for the event inserting, i put an hidden field for the artists ids:

 <%= event_form.text_field :artist_ids %>

If i inserted manually the value "8,9,10" (ids associated to 2 rows of humans) and i submit the form, in the controller i obtain only 8.

Why?

How can i do?

thanks

A: 

In your "new" action how do you find the artist_ids? You will need to use the same query in the action that the form submits to. (So if new submits to create, you need to find the artists in the create action).

Toby Hede
+1  A: 

When you assign the string "8,9,10" to artist_ids it gets converted to an integer value:

>> a.artist_ids = '1,2,3'
=> "1,2,3"
>> a.artist_ids
=> [1]

You need to split it before you pass it to the model:

>> a.artist_ids = '1,2,3'.split(',')
=> ["1", "2", "3"]
>> a.artist_ids
=> [1, 2, 3]
Tomas Markauskas