# Collection\_select with has\_one association

**URL:** <https://www.ruby-forum.com/t/collection-select-with-has-one-association/184713>\
**Category:** Rails\
**Created:** [March 7, 2010, 8:13am UTC](https://www.ruby-forum.com/t/collection-select-with-has-one-association/184713 "2010-03-07T08:13:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [March 7, 2010, 8:13am UTC](https://www.ruby-forum.com/t/collection-select-with-has-one-association/184713/1 "2010-03-07T08:13:51Z")

</div>

Hi,  
I try to create a select menu with all the city available in my shop  
form. I’ve been able to populate the select menu but the city\_id isn’t  
stored in the shop table. There must be an error in my view regarding  
that i’m not sure to understand each parameters of the  
collection\_select.

Shop.rb  
class Shop \< ActiveRecord::Base  
attr\_accessible :total\_visit, :friends\_invited

has\_and\_belongs\_to\_many :products  
has\_and\_belongs\_to\_many :owners  
has\_many :credits  
has\_many :visits  
belongs\_to :city  
end

City.rb  
class City \< ActiveRecord::Base  
attr\_accessible :name, :continent

has\_one :shop  
end

Shop view  
\<% form\_for @shop do |f| %\>

\<%= collection\_select(@shop,:city\_id, City.find(:all), :id, :name, options ={:prompt =\> "-Select a city"}, :class =\>"") %\>

\<%= f.submit "Submit" %\>

\<% end %\>

Best,  
Greg

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [March 7, 2010, 9:33am UTC](https://www.ruby-forum.com/t/collection-select-with-has-one-association/184713/2 "2010-03-07T09:33:15Z")

</div>

Hello,

In your shop view change collection line to: \<%=  
f.collection\_select(:city\_id, City.find(:all), :id, :name,  
options ={:prompt =\> “-Select a city”}, :class =\>"") %\>
