ruby-on-railsrails-geocoder

Rails - geocode by continent, country and city


I am trying to put together a directory of some of the worlds best cities around the world.

I have:

    ContinentsController < ApplicationController
      def index 
      end 

      def show 
      end 
    end

    CountriesController < ApplicationController
      def index 
      end 

      def show 
      end 
    end

    CitiesController < ApplicationController
      def index 
      end 

      def show 
      end
    end

as well as:

    class Continent < ApplicationRecord
      has_many :countries
      validates :continent_name, presence: true
    end

    class Country < ApplicationRecord
      belongs_to :continent 
      has_many :cities
      validates :country_name, presence: true 
      validates :continent_id, presence: true 
    end

    class City < ApplicationRecord
     belongs_to :continent 
     belongs_to :country
     validates :city_name, presence: true 
     validates :country_id, presence: true 
     validates :continent_id, presence: true
    end

I am using the geocoder gem. How would I geocode this? City needs to be geocoded by both country_name and city_name because cities in different parts of the world can share the same name. One example is Saint Petersburg located in both Russia and the US.

    class City < ApplicationRecord
     geocoded_by :city_name
     after_validation :geocode, if: :city_name_changed?
    end

This doesn't work in the case of Saint Petersburg as it's only geocoding the city_name, not the country_name.

Thanks a lot in advance!


Solution

  • You can do something like this:

    class City < ApplicationRecord
     geocoded_by :address
     after_validation :geocode, if: :city_name_changed?
    
     def address
       "#{city_name}, #{country_name}"
     end
    end
    

    The documentation shows this:

    def address
      [street, city, state, country].compact.join(', ')
    end
    

    https://github.com/alexreisner/geocoder#geocoding-objects