Passing Back Collection Values From Associations

8 views
Skip to first unread message

Vladiim

unread,
Jan 20, 2012, 2:11:16 AM1/20/12
to SimpleForm
I've got a form for a booking with a user association

To display the collection of user names I'm using the following:
= f.association :client, :collection => current_user.teaching.collect
{ |c| c.username }

The issue: although this works it doesn't pass the selected user's id
back to the db (it's passing back the username) making the booking
invalid (note: the id passed back is defaulting to 0)

Which additional params ensure I'm sending back the selected user's id
rather than their username?

Carlos Antonio da Silva

unread,
Jan 20, 2012, 8:07:03 AM1/20/12
to plataformate...@googlegroups.com
You have to map the collection to a pair of text and value attributes, ie an array containing the labe and the value for each option tag that will be generated. Try this:

  f.association :client, :collection => current_user.teaching.collect { |c| [c.username, c.id] }
--
At.
Carlos Antonio

Vladiim

unread,
Jan 20, 2012, 6:18:56 PM1/20/12
to SimpleForm
As always thanks for the fast & excellent answer

On Jan 21, 12:07 am, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> You have to map the collection to a pair of text and value attributes, ie
> an array containing the labe and the value for each option tag that will be
> generated. Try this:
>
>   f.association :client, :collection => current_user.teaching.collect { |c|
> [c.username, c.id] }
>

Carlos Antonio da Silva

unread,
Jan 20, 2012, 7:08:42 PM1/20/12
to plataformate...@googlegroups.com
Glad to know it is working :)
--
At.
Carlos Antonio
Reply all
Reply to author
Forward
0 new messages