RailsCasts Pro episodes are now free!

Learn more or hide this

Nicolò G.'s Profile

GitHub User: nickgnd

Comments by Nicolò G.

Avatar

Yes, you can create a method in Country model that sorts the states by country_name and states_name (execute a query sql), and later call it in your form.

Example: if your new method is called "states_by_country_name_and_state_name"

in the form

/app/views/people/new.html.erb
<%= f.grouped_collection_select :state_id, Country.order(:name), :states_by_country_name_and_state_name, :name, :id, :name, include_blank: true %>