Forum: Ruby on Rails How to have multiple fields appear in collection_select?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7238038e32eb30a16098833e3d61cb19?d=identicon&s=25 Bob Boyken (bboyken)
on 2006-03-16 21:56
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?
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-17 02:56
(Received via mailing list)
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
7238038e32eb30a16098833e3d61cb19?d=identicon&s=25 Bob Boyken (bboyken)
on 2006-03-17 03:25
Craig White 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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-17 03:47
(Received via mailing list)
On Fri, 2006-03-17 at 03:25 +0100, Bob Boyken wrote:
> Craig White 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 Silva 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  ;-)  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
0454b2d73007699b409d4d7a2bcf0b5f?d=identicon&s=25 oom tuinstoel (Guest)
on 2006-03-17 08:38
Or you could make a method in your model that returns "#{firstname}
#{lastname}"
7238038e32eb30a16098833e3d61cb19?d=identicon&s=25 Bob Boyken (bboyken)
on 2006-03-17 16:30
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
0454b2d73007699b409d4d7a2bcf0b5f?d=identicon&s=25 Oom Tuinstoel (Guest)
on 2006-03-17 18:53
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:
<div class="row">
    <label for="child_parent_id">Parent</label>
    <%= collection_select(:child, :parent_id, @parents, :id, :fullname)
%>
</div>
This topic is locked and can not be replied to.