Multi-selects and many-to-many relationships

Hello,
So far I’ve been making selects in the form that end being automatically
mapped to the relationship one-to-many on the database.
Now I am trying to make a multi-select that maps to a many-to-many
relationship. Is that possible ?
I have Courses and Books, many to many. So, when editing a book I
output:

<%= select ‘book’, ‘courses’, Course.find(:all, :order => :name).collect
{|p|
[ p.name, p.id ] }, {:include_blank => true}, {:class
=> “text-input”, :multiple => true, :size => 10 } %>

It prints correctly, but when saving I get “Course expected, got
String”. I’ve
tried with ‘course_ids’ instead of ‘courses’ but then I get “undefined
method
`course_ids’ for #Book:0xb6eabf94” when generating the form.

How should I do it ?

Thank you.

Pupeno wrote:

It prints correctly, but when saving I get “Course expected, got String”. I’ve
tried with ‘course_ids’ instead of ‘courses’ but then I get “undefined method
`course_ids’ for #Book:0xb6eabf94” when generating the form.

How should I do it ?

Yes, association collections should be defining a getter as well as a
setter
for the _ids method. You can add one manually:

def course_ids
courses.map(&:id)
end


We develop, watch us RoR, in numbers too big to ignore.