# Multiple Select example?

**URL:** <https://www.ruby-forum.com/t/multiple-select-example/58876>\
**Category:** Rails\
**Created:** [April 26, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/multiple-select-example/58876 "2006-04-26T20:23:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@brez](https://www.ruby-forum.com/u/brez)\
**Post date:** [April 26, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/multiple-select-example/58876/1 "2006-04-26T20:23:26Z")

</div>

I’ve been trying to sort out a multiple select dropdown but haven’t  
found any good examples - anyone know of any or perhaps could make a  
suggestion [see below]

Model:

1. Task AR has\_many users
2. User AR belongs\_to task  
login/password attributes

Controller:

View:  
\<%= collection\_select(“task”, “users” , @users, “id”, “login”, {},  
{:multiple =\> true}) %\>

def new  
@task = Task.new  
@users = User.find\_all  
end

def create  
@task = Task.new(params[:task])

if @task.save  
flash[:notice] = ‘Task was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

Error:

“User expected, got String”

Seems pretty straight forward but isn’t working - any sugggestions?

Thanks.

---

<div class="post-metadata">

**Author:** ![brez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@brez](https://www.ruby-forum.com/u/brez)\
**Post date:** [April 27, 2006, 9:32am UTC](https://www.ruby-forum.com/t/multiple-select-example/58876/2 "2006-04-27T09:32:47Z")

</div>

Found it! FYI if it comes up again:

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Michael K. wrote:  
| Nice, are there any functions like this for handling one to many  
| relations? i.e a User has\_and\_belongs\_to\_many :groups.

collection\_select requires a source object and an instance method. With  
has\_many, this method can be the foreign key. But with  
has\_and\_belongs\_to\_many there is no corresponding instance method since  
the keys are in a separate join table, so you’ll want to manage the  
parameters yourself. Here’s an example using  
options\_from\_collection\_for\_select to create a multiple select:

class User \< ActiveRecord::Base  
~ # id, name fields  
~ has\_and\_belongs\_to\_many :groups  
end

class Group \< ActiveRecord::Base  
~ # id, name fields  
~ has\_and\_belongs\_to\_many :users  
end

class UserController \< AbstractApplicationController  
~ model :user  
~ def edit  
~ case @request.method  
~ when :get  
~ @user = User.find(@params[‘id’])  
~ @available\_groups = Groups.find\_all - @user.groups  
~ when :post  
~ @user = User.find(@params[‘user’][‘id’])  
~ @user.groups \<\< Group.find(@params[‘add\_groups’])  
~ redirect\_to :action =\> ‘show’, :id =\> @user.id  
~ end  
~ end  
end

views/user/edit.rhtml  
\<%= form\_tag :action =\> ‘edit’ %\>  
~ \<%= hidden\_field ‘user’, ‘id’ %\>  
~   
~ \<%= options\_from\_collection\_for\_select @available\_groups, ‘id’,  
‘name’ %\>  
~

Check out the relevant FormOptionsHelper documentation:  
[http://ap.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000075](http://ap.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000075)

Best,  
jeremy  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.2.6 (Darwin)  
Comment: Using GnuPG with Thunderbird - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFBpBYeAQHALep9HFYRAk8qAKCbRIzv2GBiWGSfIsmlx1ZZYuodqQCePlV2  
9tqGiLfiZTyocwJ1U9xMTmY=  
=vrPm  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![brez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@brez](https://www.ruby-forum.com/u/brez)\
**Post date:** [April 27, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/multiple-select-example/58876/3 "2006-04-27T21:02:41Z")

</div>

Actually - here’s my working sample [a little cleaner then above]

view:

 \<%= options\_from\_collection\_for\_select @users, 'id', 'login' %\> 

controller:

```
@task = Task.new(@params[:task])

```

Turns out I missed this very valuable wiki entry - thanks Joel!

[http://wiki.rubyonrails.com/rails/pages/HowtoUseFormOptionHelpers](http://wiki.rubyonrails.com/rails/pages/HowtoUseFormOptionHelpers)
