# Concatenate columns from a database to one string?

**URL:** <https://www.ruby-forum.com/t/concatenate-columns-from-a-database-to-one-string/89807>\
**Category:** Rails\
**Created:** [March 22, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/concatenate-columns-from-a-database-to-one-string/89807 "2007-03-22T21:27:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ngng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a88e4f/32.png) [@ngng](https://www.ruby-forum.com/u/ngng)\
**Post date:** [March 22, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/concatenate-columns-from-a-database-to-one-string/89807/1 "2007-03-22T21:27:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ngng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a88e4f/32.png) [@ngng](https://www.ruby-forum.com/u/ngng)\
**Post date:** [March 22, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/concatenate-columns-from-a-database-to-one-string/89807/2 "2007-03-22T21:40:27Z")

</div>

On Mar 22, 8:27 pm, Jonathan Ng [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![ngng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a88e4f/32.png) [@ngng](https://www.ruby-forum.com/u/ngng)\
**Post date:** [March 22, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/concatenate-columns-from-a-database-to-one-string/89807/3 "2007-03-22T21:41:49Z")

</div>

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

thanks Chris!

Chris M. wrote:

> On Mar 22, 8:27 pm, Jonathan Ng [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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

---

<div class="post-metadata">

**Author:** ![ngng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a88e4f/32.png) [@ngng](https://www.ruby-forum.com/u/ngng)\
**Post date:** [March 23, 2007, 1:52am UTC](https://www.ruby-forum.com/t/concatenate-columns-from-a-database-to-one-string/89807/4 "2007-03-23T01:52:11Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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

---

<div class="post-metadata">

**Author:** ![ngng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a88e4f/32.png) [@ngng](https://www.ruby-forum.com/u/ngng)\
**Post date:** [March 27, 2007, 1:53am UTC](https://www.ruby-forum.com/t/concatenate-columns-from-a-database-to-one-string/89807/5 "2007-03-27T01:53:25Z")

</div>

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

Chris
