Xeen
Xeen

Reputation: 7003

How to use multiple options for dropdown select value in Rails?

<%= f.collection_select :sportist_id, Sportist.order("CREATED_AT DESC"),:id,:name, include_blank: true %>

I'd like to be able to select a sportist by name AND surname, but as far as I can figure out, only one option for the dropdown value can persist. Is it possible to somehow combine :name + :surname so that they work?

Upvotes: 1

Views: 888

Answers (1)

Marcelo Ribeiro
Marcelo Ribeiro

Reputation: 1738

You could create a full_name attribute in your model class:

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

and use it in your select:

<%= f.collection_select :sportist_id, Sportist.order("CREATED_AT DESC"),:id,:full_name, include_blank: true %>

Upvotes: 1

Related Questions