How to get list of all countries and cities in rails?
Use the aptly named city-state gem.
As seen by the README, you can do:
CS.cities(state, country)
If you want all cities in a given country, regardless of state, you could do:
CS.states(country).keys.flat_map { |state| CS.cities(state, country) }
To list all countries:
CS.countries
The cool thing is that it uses data from MaxMind so you can be sure the list will be up to date. To refresh your list, you can run:
CS.update
The only drawback is that it requires rails (active support specifically) to run, but this isn't a problem in your case.
Rails get unique country with cities
Hey you can try this way using group
it gives you all distinct records
@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
It will give you output like
[{:country => "US", :city => "New York"}..]
If you want to again group it by country then used like
cchs = @countries_cities.group_by{|cc| cc["country"]}
Convert above multidimensional array to hash using
@country_cities_hash = = Hash[*cchs]
In your view file as
<% @country_cities_hash.each do |country, cities| %>
<li class="input"><a href="#"><%= country %></a></li>
<% cities.each do |city| %>
<li class="input"><a href="#"><%= "#{city}(#{country})" %></a></li>
<% end %>
<% end %>
Rails: Get Country (or any other) attribute of 'Users where city is unique'
I think this should do it:
User.select("country, city").uniq
This should return an array of User
objects where only country
and city
is populated.
Then you can map it into something you need, i.e.:
User.select("country, city").uniq.map { |u| "#{u.country} - #{u.city}" }
for instance...
UPDATE
Answering your comment:
Yes, you can just add the lat and lng fields you need like so:
# remember afterwards to only access the fields you're actively selecting here,
# otherwise you'll get an exception
@users = User.select("country, city, lat, lng").uniq.map { |u| { country: u.country, city: u.city, lat: u.lat, lng: u.lng } }
For easy use add a method to your User
model that returns a string as you need it, i.e.:
def country_city
"#{country} - #{city}"
end
And in your view(s) you can use the collection_select
helper method to only use the fields you want (by using the new method in the model) for your select but still have the lat
and lng
fields in the hash-array.
collection_select(:name_of_view_instance, :name_of_select_field, @users, :id, :country_city)
This gives you an HTML output like the following:
<select name="name_of_view_instance[name_of_select_field]">
<option value="1" selected="selected">United States - Boston</option>
<option value="2">United States - New York</option>
<option value="3">United States - San Francisco</option>
<!-- and so on -->
</select>
See the documentation of the helper method at above link.
Rails GeoIP return country/city list
.dat file is a database file where, in one way, contents are stored in table form, but in compressed form. Just find out which columns refers to countries and cities. In following links, there are similar questions:
Ruby sorting a .dat file by column
How can I handle large files in Ruby?
Getting a user country name from originating IP address with Ruby on Rails
Refer to above links and modify the codes to suit your needs.
Related Topics
Trouble on Rendering a Template Passing a Local Variable
I18N: Error Message Localization for Particular Model
Difference Between String.Scan and String.Split
After Installing a Gem Within a Script, How to Load the Gem
Stream and Unzip Large CSV File with Ruby
Split String Without Removing Delimiter
Raise Exception When Accessing Attributes That Doesn't Exist in Openstruct
Chef and Erb Templates. How to Use Boolean Code Blocks
How I Know My Document's Size Inside Mongodb with the Ruby Driver
Convert HTML to Plain Text (With Inclusion of <Br>S)
Invoke Delayed_Job Capistrano Tasks Only on Specific Servers
Best Way to Work with Large Amounts of CSV Data Quickly
Whenever Gem: I Set :Output But the Logfile Doesn't Show Up Where I'D Expect It To
Why Is "❨╯°□°❩╯︵┻━┻" with Such an Encoding Used for a Method Name
Rspec: How to Write a Test That Expects Certain Output But Doesn't Care About the Method