Populate combo box from database without repeatness

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.

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") %>

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…

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.

HI,

Its populated… Thank you verymuch…