How to have multiple fields appear in collection_select?

I have a table of employees with the fields “last_name” and
“first_name”. I would like to populate a collection select so that what
the user sees in the drop-down is “Last Name, First Name”. So far, the
only way I have figured out to do this is by using “find_by_sql” in my
model, like so:

@employees.find_by_sql(“SELECT id, concat(last_name, ', ', first_name)
AS full_name FROM employees ORDER BY full_name”)

It works fine, but someone on the irc said they thought this was the
“dirty” way to go. Any suggestions on a better way to do this?

On Thu, 2006-03-16 at 21:56 +0100, Bob Boyken wrote:

“dirty” way to go. Any suggestions on a better way to do this?


AWDWR covers this very nicely - it’s called Aggregations.

There is probably a wiki on wiki.rubyonrails.org that explains it
too…but perhaps not as well as Dave does.

Craig

Craig W. wrote:

AWDWR covers this very nicely - it’s called Aggregations.

Right on. Thanks! AWDWR Page 247. I appreciate your pointing me to the
book instead of simply saying “do this.” I’ll learn more this way.

Part of the “problem” with Rails is all the unfamiliar terminology.
That’s not a criticism. It’s just difficult sometimes to know the right
words to use when searching for answers. Fortunately, there seems to be
kind, knowlegable, and growing community.

Bob

On Fri, 2006-03-17 at 03:25 +0100, Bob Boyken wrote:

Craig W. wrote:

AWDWR covers this very nicely - it’s called Aggregations.

Right on. Thanks! AWDWR Page 247. I appreciate your pointing me to the
book instead of simply saying “do this.” I’ll learn more this way.


probably - but I may not have done you any favors as the implementation
of aggregations can be difficult in certain areas and when I asked about
it’s usage 6 weeks ago when I started…Bob S. suggested that I just
put the the aggregate name into the table anyway and there have been
many times, I wished I did that. For example, ‘validations’ on an
aggregation are not simple…searching on aggregations is never as
simple. Beware of what you wish for :wink: For the record, I’m still
hanging in there with them but they have cost me some large amounts of
time to implement methodologies where a column would have been very
easy.

Part of the “problem” with Rails is all the unfamiliar terminology.
That’s not a criticism. It’s just difficult sometimes to know the right
words to use when searching for answers. Fortunately, there seems to be
kind, knowlegable, and growing community.


I believe I expressed something similar just two days ago (terminology)
and I still am not always certain about brackets, curly brackets and
parens…which to use when/why.

Craig

Or you could make a method in your model that returns “#{firstname}
#{lastname}”

oom tuinstoel wrote:

Or you could make a method in your model that returns “#{firstname}
#{lastname}”

I saw reference to that in another post, but I’m clueless as to how to
implement it and make it work with collection_select.

Bob

Bob Boyken wrote:

oom tuinstoel wrote:

Or you could make a method in your model that returns “#{firstname}
#{lastname}”

I saw reference to that in another post, but I’m clueless as to how to
implement it and make it work with collection_select.

Bob

Bob,

you probably saw my post from a few days ago. I too was clueless then,
but hey, here I am telling you what to do. Isn’t Rails a miracle?

Here goes! In your model create a method that returns the concat of
prefix and lastname:
class Parent < ActiveRecord::Base
has_many :children

def fullname
“#{prefix} #{lastname}”
end
end

In the view for the child (probably _form.rhtml) use this:

Parent <%= collection_select(:child, :parent_id, @parents, :id, :fullname) %>