# Collection\_select - why doesn't it work?

**URL:** <https://www.ruby-forum.com/t/collection-select-why-doesnt-it-work/89497>\
**Category:** Rails\
**Created:** [March 20, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/collection-select-why-doesnt-it-work/89497 "2007-03-20T21:22:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 20, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/collection-select-why-doesnt-it-work/89497/1 "2007-03-20T21:22:19Z")

</div>

Hi all

I guess this is quite a newbie question, but I’m just stuck here and  
don’t know why.

\<%= form.collection\_select :origin\_country, Country.find(:all), :id,  
:name %\>

gives me a select list with name=“member[origin\_country]”. When  
submitting the form the following error is shown:

Country expected, got String

When I try

\<%= form.collection\_select :origin\_country\_id, Country.find(:all), :id,  
:name %\>

I get the error

undefined method `origin\_country\_id’ for #Member:0x23f52e8

So I try it with

\<%= form.select :origin\_country, Country.find(:all).map{|obj| [obj.name,  
obj.id]} %\>

but again the same “Country expected, got String” error.

As far as I know Rails expects a select list with a name  
“member[origin\_country\_id]” or something like that?

My models look like that:

class Member \< ActiveRecord::Base  
belongs\_to :origin\_country,  
:class\_name =\> ‘Country’  
end

class Country \< ActiveRecord::Base  
has\_many :originating\_members,  
:class\_name =\> ‘Member’  
end

And the foreign key column in the members table is called “country\_id”.

So where’s the mistake? Thanks a lot for help. 🙂  
Joshua

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 20, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/collection-select-why-doesnt-it-work/89497/2 "2007-03-20T23:00:07Z")

</div>

It’s a pretty simple mistake, but it’s confused by your unconventional  
association. Normally, you would assign to the association name plus  
‘\_id’, but that doesn’t match the class. And just like the error  
says, you can’t assign a string (the Country object’s ID) to the  
association (because it wants a Country object). Try a select on  
country\_id instead of origin\_country or origin\_country\_id.

By the way, I prefer to fully qualify any association that doesn’t use  
defaults.

belongs\_to :origin\_country, :class\_name =\> ‘Country’, :foreign\_key  
=\> :country\_id

Otherwise, it can be confusing to know whether the foreign key depends  
on the association name or class name (and I believe it has switched  
or will switch or something, but I could be wrong about that).

–  
-yossef

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 20, 2007, 11:20pm UTC](https://www.ruby-forum.com/t/collection-select-why-doesnt-it-work/89497/3 "2007-03-20T23:20:04Z")

</div>

Thanks a lot, very helpful!
