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 <pupeno-GAtDADarczzQT0dZR+AlfA@public.gmane.org> (http://pupeno.com)
Mark Reginald James
2006-Oct-06 21:22 UTC
Re: Multi-selects and many-to-many relationships.
Pupeno wrote:> 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 ?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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---