# Using collection\_select

**URL:** <https://www.ruby-forum.com/t/using-collection-select/98619>\
**Category:** Rails\
**Created:** [July 16, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/using-collection-select/98619 "2006-07-16T23:22:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [July 16, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/using-collection-select/98619/1 "2006-07-16T23:22:26Z")

</div>

hello,

supposing i have a User class that contains a Wibble

class User \< ActiveRecord::Base  
has\_one :wibble  
end

in my view i have  
\<%= collection\_select :user, :wibble, Wibble.find(:all), :id, :name %\>

when i post, i get an error like  
“Wibble expected, got String”

how/where am I supposed to convert this posted wibble\_id into a  
Wibble, or am I using collection\_select incorrectly?

i’ve seen some suggestions to use :wibble\_id in the select, e.g.  
\<%= collection\_select :user, :wibble\_id, Wibble.find(:all), :id, :name  
%\>

but if i do this i get an “undefined method wibble\_id” error

thanks,  
Jeff

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [July 17, 2006, 1:31am UTC](https://www.ruby-forum.com/t/using-collection-select/98619/2 "2006-07-17T01:31:01Z")

</div>

Jeff Emminger wrote:

> hello,
> 
> supposing i have a User class that contains a Wibble
> 
> class User \< ActiveRecord::Base  
> has\_one :wibble  
> end
> 
> in my view i have  
> \<%= collection\_select :user, :wibble, Wibble.find(:all), :id, :name %\>
> 
> when i post, i get an error like  
> “Wibble expected, got String”
> 
> how/where am I supposed to convert this posted wibble\_id into a  
> Wibble, or am I using collection\_select incorrectly?
> 
> i’ve seen some suggestions to use :wibble\_id in the select, e.g.  
> \<%= collection\_select :user, :wibble\_id, Wibble.find(:all), :id, :name  
> %\>
> 
> but if i do this i get an “undefined method wibble\_id” error
> 
> thanks,  
> Jeff

You’re trying to get a select list for a form? Here’s how I do it:

This line grabs entries where users are tied to groups. GroupUsers  
belongs to Group, Groups have many GroupUsers.  
\<% @groups = GroupUser.find(:all, :conditions =\>  
“user\_id=’”+session[:user\_id].to\_s+"’") %\>

Now take @groups array and for each entry in it, have it display the  
name of the group, and then embed the group ID:  
\<%= select(“group\_users”, “group\_id”, @groups.collect {|p| [  
p.group.name, p.group.id ] }, { :include\_blank =\> true }) %\>

It turns it into this html:

 Group1 Group2 Group5

Hope this helps!

-Ben L.

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [July 17, 2006, 2:22am UTC](https://www.ruby-forum.com/t/using-collection-select/98619/3 "2006-07-17T02:22:35Z")

</div>

ben,

thanks for the reply. that’s pretty much what i’m doing…  
specifically i’m trying to attach my own TimeZoneImpl class to the User.  
the User has\_one :time\_zone\_impl

my select list ends up being semantically identical - time zone name  
display with the id for each option value. the problem is that when the  
form is submitted, rails is trying to put the int id in the  
user.time\_zone\_impl slot, when it needs to be an instance of  
TimeZoneImpl instead.

perhaps i need to look into composed\_of since the User is composed of a  
TimeZoneImpl?

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [July 17, 2006, 5:44am UTC](https://www.ruby-forum.com/t/using-collection-select/98619/4 "2006-07-17T05:44:51Z")

</div>

i think i’ve solved it… using :time\_zone\_impl\_id in the  
collection\_select call is correct. my error was not having association  
field in the DB spelled correctly! 😃
