# Newbie - populating select list from db

**URL:** <https://www.ruby-forum.com/t/newbie-populating-select-list-from-db/55610>\
**Category:** Rails\
**Created:** [March 22, 2006, 1:14pm UTC](https://www.ruby-forum.com/t/newbie-populating-select-list-from-db/55610 "2006-03-22T13:14:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [March 22, 2006, 1:14pm UTC](https://www.ruby-forum.com/t/newbie-populating-select-list-from-db/55610/1 "2006-03-22T13:14:53Z")

</div>

Can’t seem to find an example of this anywhere - how do I populate a  
select list in a form from a database?

I have a form to create a new task. The task is for a client, so there  
should be a select list populated from my clients table. I thought this  
would work:  
\<%= select ‘task’, ‘client\_id’, Client.find\_all, “id”, “name” %\>

but I get this error:  
undefined method `stringify\_keys’ for “name”:String

Obviously I’m missing something…

And then is this going to work properly when I’m using the same form  
partial to edit the task?

thx

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [March 22, 2006, 1:20pm UTC](https://www.ruby-forum.com/t/newbie-populating-select-list-from-db/55610/2 "2006-03-22T13:20:15Z")

</div>

Here is what I am using:

City  
 \<%= select("search", "city", Provider.find\_all.collect {|p| [p.city, p.id] }, { :include\_blank =\> true }) %\>

It populates the select with the city names but returns the city\_id.

Nick C. wrote:

> Can’t seem to find an example of this anywhere - how do I populate a  
> select list in a form from a database?
> 
> I have a form to create a new task. The task is for a client, so there  
> should be a select list populated from my clients table. I thought this  
> would work:  
> \<%= select ‘task’, ‘client\_id’, Client.find\_all, “id”, “name” %\>
> 
> but I get this error:  
> undefined method `stringify\_keys’ for “name”:String
> 
> Obviously I’m missing something…
> 
> And then is this going to work properly when I’m using the same form  
> partial to edit the task?
> 
> thx

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [March 22, 2006, 2:10pm UTC](https://www.ruby-forum.com/t/newbie-populating-select-list-from-db/55610/3 "2006-03-22T14:10:22Z")

</div>

Kris wrote:

> Here is what I am using:
> 
> City  
> \<%= select("search", "city", Provider.find\_all.collect {|p| [p.city, p.id] }, { :include\_blank =\> true }) %\>
> 
> It populates the select with the city names but returns the city\_id.

Thanks. That works. Also found the form\_helpers section in the Agile  
Rails book, on p357. So I’ve ended up with:

\<% @clients = Client.find(:all, :order =\> “name”).map {|c| [c.name,  
c.id] } %\>  
\<%= select ‘task’, ‘client\_id’, @clients %\>

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [March 22, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/newbie-populating-select-list-from-db/55610/4 "2006-03-22T14:45:04Z")

</div>

Hi!

in the same problem with a select with some values from db:

the following select in test.rhtml:

\<%= select(:listSetting, :code, @listSettings) %\>

where

@listSettings = ListSetting.find(:all, :order =\> “code”).map {|u|  
[u.code, u.id] }

i have an observer\_field defined in test.rhml as below:

\<%= observe\_field(“listSetting\_code”,  
:update =\> “values\_span”,  
:url =\> {:action =\> :listSetting\_code}  
)  
%\>

which will observe the select object and run the code from  
ListSetting\_code method from controller.

the log after changing the select is as below:

Processing ListAdminController#listSetting\_code (for 127.0.0.1 at  
2006-03-22 14:39:17) [POST]  
Parameters: {“action”=\>“listSetting\_code”,  
“value”=\>“listSetting\_code”, “1”=\>"", “controller”=\>“list\_admin”}

2 short n00b questions:

1. what it means “1”=\>"" in the log file?
2. how do I get the value of option selected in controller method?

TIA.

Dan

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [March 22, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/newbie-populating-select-list-from-db/55610/5 "2006-03-22T18:28:31Z")

</div>

On 3/22/06, Nick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks. That works. Also found the form\_helpers section in the Agile  
> Rails book, on p357. So I’ve ended up with:
> 
> \<% @clients = Client.find(:all, :order =\> “name”).map {|c| [c.name,  
> c.id] } %\>  
> \<%= select ‘task’, ‘client\_id’, @clients %\>

Taking it one small step further, I like to put this code into helpers.

Helper code:

def all\_clients  
Client.find(:all, :order =\> “name”).map {|c| [c.name, c.id] }  
end

View:  
\<%= select ‘task’, ‘client\_id’, all\_clients %\>

– James
