Sam Joseph
2006-Apr-21 20:54 UTC
[Rails] collection_select has_and_belongs_to_many update_attributes
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