Using collection_select

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

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.

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?

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! :smiley:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs