Concatenate columns from a database to one string?

I’m trying to use collection_select to display a drop down box from a
database table, the only problem I have is, insted of displaying :name
I’d like to display a string which is a combination of all the columns
from a specific row in a table. How should I go about this?

<%= collection_select(:classified, :category_id, @categories, :id,
:name) %>

On Mar 22, 8:27 pm, Jonathan Ng [email protected]
wrote:

I’m trying to use collection_select to display a drop down box from a
database table, the only problem I have is, insted of displaying :name
I’d like to display a string which is a combination of all the columns
from a specific row in a table. How should I go about this?

<%= collection_select(:classified, :category_id, @categories, :id,
:name) %>

That fourth parameter (where you have “:name”) just specifies a method
to call on your object. So you can put any method in there, not just a
database attribute.

In other words, you could have the following method in your mode:

def summary
“#{self.id}: #{self.name}”
end

and then pass :summary to the collection_select method, and it’ll use
the values returned by that method to populate your options tags.

Chris

coming from a PHP background all this is driving me crazy. but, i love
rails :slight_smile:

thanks Chris!

Chris M. wrote:

On Mar 22, 8:27 pm, Jonathan Ng [email protected]
wrote:

I’m trying to use collection_select to display a drop down box from a
database table, the only problem I have is, insted of displaying :name
I’d like to display a string which is a combination of all the columns
from a specific row in a table. How should I go about this?

<%= collection_select(:classified, :category_id, @categories, :id,
:name) %>

That fourth parameter (where you have “:name”) just specifies a method
to call on your object. So you can put any method in there, not just a
database attribute.

In other words, you could have the following method in your mode:

def summary
“#{self.id}: #{self.name}”
end

and then pass :summary to the collection_select method, and it’ll use
the values returned by that method to populate your options tags.

Chris

Maybe I am doing this wrong, but I can’t seem to get this to work
correctly? I defined summary my my controller file, and called :summary
from the 4th parameter of the collection_select. Am I missing
something?

Chris M. wrote:

On Mar 22, 8:27 pm, Jonathan Ng [email protected]
wrote:

I’m trying to use collection_select to display a drop down box from a
database table, the only problem I have is, insted of displaying :name
I’d like to display a string which is a combination of all the columns
from a specific row in a table. How should I go about this?

<%= collection_select(:classified, :category_id, @categories, :id,
:name) %>

That fourth parameter (where you have “:name”) just specifies a method
to call on your object. So you can put any method in there, not just a
database attribute.

In other words, you could have the following method in your mode:

def summary
“#{self.id}: #{self.name}”
end

and then pass :summary to the collection_select method, and it’ll use
the values returned by that method to populate your options tags.

Chris

Whoops, I mis-counted – I think it’s actually the fifth parameter!

Chris