Forum: Ruby on Rails another collection_select, HABTM, :multiple question

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Sergio T. R. (Guest)
on 2009-02-13 08:13
i am currently working on a project and having a really tough time
getting a collection_select to work properly. this is using a multiple

this is part of a HABTM relationship, and it appears that i have
everything set up correctly.. but there is just something not working

the models i am working with are Worker and Skillset

they look like this:

,----[ worker ]
| class Worker < ActiveRecord::Base
|   has_and_belongs_to_many  :skillsets
|   belongs_to :instructor
| end

,----[ skillset ]
| class Skillset < ActiveRecord::Base
|   belongs_to :pathway
|   has_many :skills
|   has_and_belongs_to_many :workers
| end

what i am trying to do is pass a Worker to a form, and populate that
form with the methods of the object. the form looks like this:

,----[ form ]
| <% form_for :worker, @worker, :url => {:action => 'update_worker', :worker_id =>} do |f| -%>
| with normal form things that look like this:
| <p>
|   <%= f.label :last_name  %><br />
|   <%= f.text_field :last_name %>
| </p>
| with a collection_select that looks like this:
| <%= collection_select(:worker, :skillsets, Skillset.find(:all), :id, :title, {}, 
{:multiple => true}) %>
| <%= f.submit "Update"  %>
| <% end -%>

what i am THINKING should happen is that:
+ since the form hauls in a worker, i should be able to load up the
  collection box with all the skillsets and have the skillset that
  belong to the worker selected.

what does happen:
+ all other fields behave as they should. this is a good thing.
+ when the select box is rendered, it it shows all the skillsets
  correctly, but, the correct ones, the ones that are part of the
  Worker model are not selected. nothing is selected.

i have tried all sorts of permutations of this, and i am sure i am
really close, but i just need a nudge in the right direction. thanks!
Mark Reginald J. (Guest)
on 2009-02-13 10:35
(Received via mailing list)
Sergio R. wrote:

>   Worker model are not selected. nothing is selected.

collection_select(:worker, :skillset_ids, ...

Rails Wheels - Find Plugins, List & Sell Plugins -
Sergio T. R. (Guest)
on 2009-02-13 15:46
> collection_select(:worker, :skillset_ids, ...

holy mac!

this worked spot on..

i read and reread the documentation a million times, and didn't get

thanks so much!
This topic is locked and can not be replied to.