Forum: Ruby on Rails collection_select has_and_belongs_to_many update_attributes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sam J. (Guest)
on 2006-04-22 00:55
(Received via mailing list)
I know there have been threads on this before but I was thinking
shouldn't multiple collection_select automatically update habtm
relations when you update_attributes?

In order to get multiple collection_select to work I use:

collection_select(:user, :group_ids, Group.find(:all), :id,
:name,{},{:multiple=>'',:name=>'group[id][]'})

In fact if I don't set the :name field like this I get an error:

undefined method `each' for #<Group:0x40ad2ee4>
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1501:in
`method_missing'

which suggests that ActiveRecord is expecting to loop through this list
of groups. However with the :name field set as above the error goes
away, but the habtm relation doesn't get updated.

Actually as I was writing this I just worked out and tested the
solution, what's needed is the following:

collection_select(:user, :group_ids, Group.find(:all), :id,
:name,{},{:multiple=>'',:name=>'user[group_ids][]'})

Now this is something that should go in the rails recipes book!

It would rock even harder if the created_on stuff held for the habtm
join table as well ...

I made a doc patch and submitted it:

http://dev.rubyonrails.org/ticket/4837

CHEERS> SAM
This topic is locked and can not be replied to.