Following were my query to get list of countries along with cities:
@countries_cities = Product.joins(:user).where("country is not null and country <> ''").where("city is not null and city <> ''").where(:users => {:merchant_status => 1}).group(:country, :city).select("country,city").as_json
The output result were as follow:
Object[city:"Bangkok",country:"Thailand"],Object[city:"Phuket",country:"Thailand"],Object[city:"Malaysia",country:"Kuala Lumpur"],Object[city:"Malaysia",country:"Penang"],Object[city:"Shanghai",country:"China"],Object[city:"Beijing",country:"China"]
cchs = @countries_cities.group_by{|cc| cc["country"]}
@search_location_country = cchs
And the view is:
<ul id="color-dropdown-menu" class="dropdown-menu dropdown-menu-right" role="menu">
<% @search_location_country.each do |country, cities| %>
<li class="input" style="background:#ECECEC; "><a href="#" style="font-weight: bold;"><%= country.upcase %></a></li>
<% cities.each do |city| %>
<li class="input"><a href="#"><%= city["city"].titleize %></a></li>
<% end %>
<% end %>
</ul>
Now the Drop down result follow this pattern:
Thailand
-Bangkok
-Phuket
Malaysia
-Kuala Lumpur
-Penang
China
-Beijing
-Shanghai
How can I ensure that Malaysia
will always place at the top of the drop down lists? Thanks!!