Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get multiple fields for options_from_collection_for_select

I have the following in a select_tag. It works fine. (I'm using select_tag because it is for a search not tied to a model.)

options_from_collection_for_select(@customers, :id, :first_name)

The current HTML output is:

<option value="4">Fred</option>

But I want:

<option value="4">Fred Flintstone</option>

I want to display the full name instead of just the first name. I can't seem to get it to use both fields "first_name" and "last_name" nor can I figure out how to get it to call a method where I concatenate the two fields. How can I get this to work?

like image 479
circle1 Avatar asked Jul 22 '13 16:07

circle1


1 Answers

This can also be done in this way, you don't need to write a method in your model.

options_from_collections_for_select(
  @customers, :id, ->(ob) { "#{ob.first_name} #{ob.last_name}" }
)
like image 165
theabhisheksoni Avatar answered Oct 12 '22 11:10

theabhisheksoni