master
 1class Location < ApplicationRecord
 2  belongs_to :locatable, polymorphic: true
 3  validates :locatable, presence: true
 4
 5  GEO_IP=GeoIP.new('config/GeoLiteCity.dat')
 6
 7  class << self
 8    def build_from_ip(ip)
 9      city = GEO_IP.city(ip)
10      return nil unless city
11      Location.new(
12        latitude: city.latitude,
13        longitude: city.longitude,
14        city: city.city_name,
15        country: city.country_name
16      )
17    end
18  end
19end