Forum: Ruby on Rails options_from_collection_for_select with complex name

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
gabordemeter (Guest)
on 2007-06-13 00:49
(Received via mailing list)
Hi everyone,

I just came across this problem and don't know how to solve it. I have
a select list:

<%= select_tag :tech_lead, options_from_collection_for_select
(@employees, "id", "first_name", @project.tech_lead.to_i), {:class =>
"text_field_project"} %>

As you will notice, I have an @employees collection passed to the
"options_from_collection_for_select" which contains a "first_name" and
"last_name" attributes.

Right now my select list properly display the first name of all
employees (as you can see in the code above), however, I'd like to
make it display "first_name space last_name" (like "John S.").

How can I make this happen in the above code?


Rob B. (Guest)
on 2007-06-13 01:25
(Received via mailing list)
On Jun 12, 2007, at 4:48 PM, gabordemeter wrote:

> Thanks,
> Gabor

Add this to your model and tell the options_... call to use
'full_name' rather than 'first_name'

def full_name
   [ self.first_name, self.last_name ].compact.join(' ')

Or perhaps

def full_name
   "#{self.first_name} #{self.last_name}".strip


Rob B.
gabordemeter (Guest)
on 2007-06-13 03:40
(Received via mailing list)
Thanks Rob! Worked great!

On Jun 12, 2:24 pm, Rob B. <removed_email_address@domain.invalid>
This topic is locked and can not be replied to.