Dropdown with concatenated columns

What is the best way to create a drop down where the viewable text in a
concatenation of 2 or more columns?

For instance, I hane a lookup table with these columns.
Model FOO
columns: id , name, phone

In my drop select tag, I’d like the user to see:

“name1 phone1”
“name2 phone2”
etc…

I know I can do this using find_by_sql . …
But, isn’t there a more elegant way?

“name2 phone2”
etc…

I know I can do this using find_by_sql . …
But, isn’t there a more elegant way?

class FOO
def name_plus_phone
self.name + ’ ’ + self.phone
end
end

@names = FOO.find(:all)

<%= select_tag ‘blah’, options_from_collection_for_select(@names ‘id’,
‘name_plus_phone’) %>

-philip

On Tuesday, July 25, 2006, at 10:52 PM, Philip H. wrote:

“name2 phone2”

@names = FOO.find(:all)

<%= select_tag ‘blah’, options_from_collection_for_select(@names
‘id’, ‘name_plus_phone’) %>

-philip


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

<%= select ‘model’, ‘name’, @names.map {|x| [“#{x.name} - #{x.phone}”,
x.id]} %>

This works too, and set’s the ‘name_id’ attribute to the selected id.

_Kevin
www.sciwerks.com

Nathan, is this how it should be used?

Class Zipcode < ActiveRecord::Base
def self.city_zip
composed_of :cityzip, :mappings => w%(city zipcode)
end
end

azip = Zipcode_find_by_zipcode(‘98055’)

azip.city_zip

I’m getting a ‘method not found’ error.
-Larry

On 2006-07-26 00:05:32 -0400, Kevin O.
[email protected] said:

end
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

<%= select ‘model’, ‘name’, @names.map {|x| [“#{x.name} - #{x.phone}”,
x.id]} %>

This works too, and set’s the ‘name_id’ attribute to the selected id.

_Kevin
www.sciwerks.com

You also can use composed_of in your model to make your model easier to
use. You could create a class called ModelAndPhone and then have it
combine the name and phone during to_s.

I know it sounds complicated, but I like it.

://
Nathan H.