4

I finally figured out how to implement Dynamic Select menus using this tutorial.

Everything works, But how does one organize the Cities in the Dropdown by Name....

Below is all of the code I've written. (Please let me know if you need any further information)

New to rails please help :)

VIEWS

<%= simple_form_for ([@book, @rating]) do |f| %>

  <div class="field">
    <%= f.collection_select :state_id, State.order(:name),  :id, :name, {:include_blank=> "Select a State"}, {:class=>'dropdown'} %>
  </div>


  ### I would like the order of the cities displayed in the drop down to be alphabetized 
  <div class="field">
    <%= f.grouped_collection_select :city_id, State.order(:name), :cities, :name, :id, :name, {:include_blank=> "Select a City"}, {:class=>'dropdown'} %>
  </div>        

<% end %>
Bug
  • 2,576
  • 2
  • 21
  • 36
Serge Pedroza
  • 2,160
  • 3
  • 28
  • 41

5 Answers5

7

Option 1: In your City model, add a default scope that directs cities to be returned in alphabetical order:

# app/models/city.rb
default_scope :order => 'cities.name ASC'

Collections of City objects will, by default, be returned in alphabetically by name.

Option 2: Define a named scope in your State model that returns cities in alphabetical order as an association on a State object:

# app/models/state.rb
scope :cities_by_name, -> { cities.order(name: :asc) } # Rails 4

scope :cities_by_name, cities.order("name ASC") # Rails 3

Then, pass your scoped query to your grouped_collection helper:

f.grouped_collection_select :city_id, State.order(:name), :cities_by_name, :name, :id, :name, {:include_blank=> "Select a City"}, {:class=>'dropdown'}
zeantsoi
  • 25,857
  • 7
  • 69
  • 61
  • May I ask which solution you implemented? – zeantsoi Dec 24 '13 at 08:10
  • i tried both, and they both worked. but I implemented the first one. thanks again – Serge Pedroza Dec 24 '13 at 08:22
  • The second one doesn't work anymore with Rails 4. Anybody know a workaround without making the scope a function? – Frank Groeneveld Feb 28 '14 at 07:35
  • In rails 3, for the scope example, this gives me -> undefined local variable or method `categories' for #. I have a class that has_many :categories. and Category belongs_to :type, which is what I'm doing the grouped selection on. Not sure if anyone came across this – jrich Oct 15 '14 at 00:45
5

With Rails 4 :

# app/models/city.rb
scope :ordered_name, -> { order(name: :asc) }

# app/models/state.rb
has_many :cities, -> { ordered_name }
Grant Trevor
  • 1,052
  • 9
  • 23
3

If using Rails 5.X, you can use default_scope, in which the syntax is slightly different than in @zeantsoi answer.

default_scope { order('cities.name ASC') }

grizzthedj
  • 7,131
  • 16
  • 42
  • 62
1

How about using default_scope with ordering for City model?

Or creating a State scope like that:

scope :ordered_cities, ->{ cities.order(:name) }

and than changing your select to

f.grouped_collection_select :city_id, State.order(:name), :ordered_cities, :name, :id, :name, {:include_blank=> "Select a City"}, {:class=>'dropdown'}
Bart
  • 2,606
  • 21
  • 32
0

Like others in this thread, I had issues getting this to work with scope. Instead I got this working in Rails 5 by adding another association to the State model:

has_many :cities_by_name, -> { order(:name) }, class_name: 'City'