# Populate dropdown

**URL:** <https://www.ruby-forum.com/t/populate-dropdown/182665>\
**Category:** Rails\
**Created:** [February 1, 2010, 9:48am UTC](https://www.ruby-forum.com/t/populate-dropdown/182665 "2010-02-01T09:48:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_I\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@M\_I\_R](https://www.ruby-forum.com/u/M_I_R)\
**Post date:** [February 1, 2010, 9:48am UTC](https://www.ruby-forum.com/t/populate-dropdown/182665/1 "2010-02-01T09:48:37Z")

</div>

Hi,  
I am new for RoR i am using  
f.select for dropdown and want to populate it from database table  
i have a function in cities model which retrieve all cities and i am  
doing the follwoing to populate the dropdown  
f.select :city\_id , @cities  
but it display a strange output with some # etc  
how i will do it and i dont want to use collection\_select so thats  
mean how to write the function  
def self.find\_all\_cities  
find(:all)  
end

thats how my function is how can i modify so that it gives me s refine  
array so that it did my business

Regards,

---

<div class="post-metadata">

**Author:** ![M\_I\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@M\_I\_R](https://www.ruby-forum.com/u/M_I_R)\
**Post date:** [February 1, 2010, 10:00am UTC](https://www.ruby-forum.com/t/populate-dropdown/182665/2 "2010-02-01T10:00:43Z")

</div>

M I R wrote:

> Hi,  
> I am new for RoR i am using  
> f.select for dropdown and want to populate it from database table  
> i have a function in cities model which retrieve all cities and i am  
> doing the follwoing to populate the dropdown  
> f.select :city\_id , @cities  
> but it display a strange output with some # etc  
> how i will do it and i dont want to use collection\_select so thats  
> mean how to write the function  
> def self.find\_all\_cities  
> find(:all)  
> end
> 
> thats how my function is how can i modify so that it gives me s refine  
> array so that it did my business
> 
> Regards,

You can use this way

\<%= collection\_select(:city, :id, City.find\_all\_cities, :id, :city) %\>

i have a working craigslist clone here:

> **[GitHub - railslist/craigslist-clone: railslist is a craigslist clone](https://github.com/railslist/craigslist-clone)**
>
> railslist is a craigslist clone. Contribute to railslist/craigslist-clone development by creating an account on GitHub.

download, install and play to learn more.

---

<div class="post-metadata">

**Author:** ![M\_I\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@M\_I\_R](https://www.ruby-forum.com/u/M_I_R)\
**Post date:** [February 1, 2010, 2:23pm UTC](https://www.ruby-forum.com/t/populate-dropdown/182665/3 "2010-02-01T14:23:01Z")

</div>

M I R wrote:

> i was expecting some magic here  
> The edit page should show the correct selection by “magic” if the form  
> is correct.  
> Could you show us the edit form that isnt working as expected?

---

<div class="post-metadata">

**Author:** ![M\_I\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@M\_I\_R](https://www.ruby-forum.com/u/M_I_R)\
**Post date:** [February 1, 2010, 1:15pm UTC](https://www.ruby-forum.com/t/populate-dropdown/182665/4 "2010-02-01T13:15:15Z")

</div>

its ok for now, but when i go to edit page how will i populate the  
dropdown while the selected option will be the one coming from  
database  
suppose i have 3 cities  
ID City

1. 

```
  London

```

2. 

```
  Bristol

```

3. 

```
  Cambridge

```

when i was adding record i select Bristol with id 2 now when i go to  
edit page this one should be selected i was expecting some magic here  
{symfony apply this magic} but just the dropdown comes with first  
value selected, how i will handle it manualy and if there is some  
magic then please help me for either case

Regards,
