# Populate combo box from database without repeatness

**URL:** <https://www.ruby-forum.com/t/populate-combo-box-from-database-without-repeatness/194657>\
**Category:** Rails\
**Created:** [September 8, 2010, 4:14pm UTC](https://www.ruby-forum.com/t/populate-combo-box-from-database-without-repeatness/194657 "2010-09-08T16:14:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 8, 2010, 4:14pm UTC](https://www.ruby-forum.com/t/populate-combo-box-from-database-without-repeatness/194657/1 "2010-09-08T16:14:32Z")

</div>

Hi,

I have a column of data with repeated as mentioned below.  
_column\_name_  
Acer  
Lenova  
HP  
Lenova  
Acer  
Acer  
Lenova

I need to populate this column in a combo box without repeated data as  
mentioned below

_combo box_  
Acer  
Lenova  
HP

I tried as

\<%= collection\_select(:column\_name, TableName.all)%\>

“table\_names” is a table and “column\_name” is a column needed to  
populate.

–  
With Regards  
Palani Kannan. K

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 8, 2010, 5:51pm UTC](https://www.ruby-forum.com/t/populate-combo-box-from-database-without-repeatness/194657/2 "2010-09-08T17:51:25Z")

</div>

a. Get your DB Access out of the views…

b. Read The Fine Manual on collection\_select

c. Controller:

@vendors = TableName.find(:all).uniq or some variant thereof

With a lot of records, you might want to use a find\_by\_sql and let the  
DB do the work with a “SELECT DISTINCT…”

d. View:

\<%= collection\_select(“model\_to\_populate”, “column\_to\_populate”,  
@vendors, “id”, “vendor\_name\_field”) %\>

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 9, 2010, 12:04pm UTC](https://www.ruby-forum.com/t/populate-combo-box-from-database-without-repeatness/194657/3 "2010-09-09T12:04:43Z")

</div>

Hi, Ar Chron,

It works… Thank you for kind help. But, I am unable to use params[:id]  
to  
get the selected value from collection\_select. how i can get selected  
value  
to params.

I need the flow  
collection\_select (value selected) → param[:id]-\> find SQL row…

On 8 September 2010 17:51, Ar Chron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
With Regards  
Palani Kannan. K  
Office: +49-531-2616-226  
Mobile: +4917647098166

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 9, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/populate-combo-box-from-database-without-repeatness/194657/4 "2010-09-09T16:39:01Z")

</div>

PalaniKannan K wrote:

> But, I am unable to use params[:id] to get the selected value  
> from collection\_select. how i can get selected value to params.
> 
> I need the flow  
> collection\_select (value selected) -\> param[:id]-\> find SQL row…

The selected value is in params already, just not where you’re looking.

Take a look in your log at the post request that rails receives when you  
submit the form… this information is all readily available there.

You should see something like:  
Parameters: {“utf8”=\>“blah1”, “authenticity\_token”=\>“blah2”,  
“model\_to\_populate”=\>{“column\_to\_populate”=\>“blah3”,…},  
“commit”=\>“blah4”, “id”=\>“blah5”}

In your controller code,

params[:id] is the id of the ‘model\_to\_populate’ (blah5)

params[:model\_to\_populate][:column\_to\_populate] is the value stored by  
the collection\_select statement in the view (blah3)

Parsing through and understanding your log files is something everyone  
should be comfortable with. It’s usually the first stop in performing  
triage on unexpected behaviors.

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 9, 2010, 4:47pm UTC](https://www.ruby-forum.com/t/populate-combo-box-from-database-without-repeatness/194657/5 "2010-09-09T16:47:33Z")

</div>

HI,

Its populated… Thank you verymuch…

On 9 September 2010 16:39, Ar Chron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Take a look in your log at the post request that rails receives when you
> 
> You received this message because you are subscribed to the Google G.  
> “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
With Regards  
Palani Kannan. K  
Office: +49-531-2616-226  
Mobile: +4917647098166
