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.


With Regards
Palani Kannan. K

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…

On 8 September 2010 17:51, Ar Chron [email protected] wrote:

To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected][email protected]
.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


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

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…

On 9 September 2010 16:39, Ar Chron [email protected] 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 [email protected].
To unsubscribe from this group, send email to
[email protected][email protected]
.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


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