Как сделать чтобы при создание модели Place, автоматически добавлялся континент страны?

Я использую Ruby on Rails 5.2 и Mongoid 7.0
У меня есть модель Place где с помощью Geocoder определяется название страны по координатам, как сделать так чтобы после этого можно было добавить к стране континент (Asia, Africa, North America, South America, Antarctica, Europe, Australia) где она находится?

class Place
  include Mongoid::Document
  include Mongoid::Timestamps
  include Geocoder::Model::Mongoid
  field :coordinates, type: Array
  field :latitude, type: Float
  field :longitude, type: Float
  field :created_at, type: Time

  field :country, type: String
  field :continent, type: String
 
  before_validation :initialize_coordinates

  def initialize_coordinates
    self.coordinates = [self.longitude.to_f, self.latitude.to_f]
  end

  def set_coordinates
    [self.longitude, self.latitude].compact.join(', ')
  end

  reverse_geocoded_by :coordinates do |obj,results|
    if geo = results.first
      obj.address = geo.address
      obj.city = geo.city || geo.state
      obj.state = geo.state
      obj.state_code = geo.state_code
      obj.postal_code = geo.postal_code
      obj.country = geo.country
      obj.country_code = geo.country_code
    end
  end
  after_validation :reverse_geocode
  after_save :update_position_for_other, if: :current_position_changed?
end


Понимаю что в моделе должно быть что-то вроде
class Place
  ......

  after_save :update_continent

    def update_continent
    cont = self.country
    case cont
    when 'United States', 'Grenada'
      'North America'
    when 'Netherlands', 'Spain'
      'Europe'
    end

    self.continent = cont
  end
 end
  • Вопрос задан
  • 58 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы