Reputation: 45
In rails guides
http://guides.rubyonrails.org/form_helpers.html#option-tags-from-a-collection-of-arbitrary-objects
we can show a select box with city name as options using the below
<%= options_from_collection_for_select(City.all, :id, :name) %>
Now my select box has below options:
I need to display options with city name,country in select box. How can I do this ?
Like this
have country_id in cities table.
Upvotes: 2
Views: 877
Reputation: 51151
First, you should implement proper method in City
model, for example: name_with_country
:
def name_with_country
"#{name}, #{country.name}"
end
second, you should use this method and include country
in your cities
query to avoid N + 1 problem:
<%= options_from_collection_for_select(City.includes(:country), :id, :name_with_country) %>
Upvotes: 3