I found a way to get collection_select to work with a HABTM
relationship, by using the following for collection select:
<%= collection_select(''user'',
''assistant_ids'',
@assistants_list, :id, :name, {}, {:multiple => true, :name
=>''user[assistant_ids][]'' } ) %>
But it seems more complicated than it should be, and not well
documented. Am I missing a simpler way to do this? Or is really a rare
thing to be using collection_select with an HABTM relationship?
(In my case, it is a self-referential habtm relationship, but that
doesn''t seem to be the issue. I abandoned the subclasses mentioned in
the description below for other reasons.)
Michael
P.S. Thanks to srjoseph who submitted this doc patch that gives this
solution: http://dev.rubyonrails.org/ticket/4837
On Jun 14, 12:58 am, Michael Slater
<m...-04BUtanlfE1BDgjK7y7TUQ@public.gmane.org>
wrote:> I''ve been beating my head on this seemingly simple problem all
evening
> and would appreciate any pointers.
>
> I have an Admin model and an Assistant model, both subclasses of User,
> and with a HABTMrelationship to each other (admins have many
> assistants, and one assistant may work for multiple admins).
>
> I have a join table called admin_assistant with columns of admin_id
> and assistant_id.
>
> In the form to create an Admin, I have:
>
> <%= f.collection_select(''assistant_id'',
> @assistants_list, :id, :name ) %>
>
> While displays fine, but when the controller create action tries to
> create the new Admin, I get:
>
> Assistant expected, got String
>
> The params have the ids for the selected assistants as an array of
> strings:
>
> "admin"=>{"name"=>"Joe Smith",
"assistants"=>["2", "3"], ...}
>
> I''ve googled all over for this and it seems most people get this
> because the column name doesn''t have _id, which I already have.
But
> none of the examples I could find usedHABTMrelationships -- does
> this technique not work with the join table? What am I missing here?
>
> Thanks for any pointers you can provide.
>
> Michael
--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---