是的,您可以使用它,默认情况下,如果提供地址和 lat/lng 地址,它将覆盖。
您可以分成两个类,并Address.near([:lat, :lng])
在保存后稍后调用。
楷模
- /model/address.rb
class Address < ApplicationRecord
#any association/validations here
geocoded_by :full_street_address
#by default it will save full address to address field.
reverse_geocoded_by :latitude, :longitude do |obj,results|
if geo = results.first
#customize as you want
obj.street = geo.address.split(',')[0]
obj.city = geo.city
obj.zip = geo.postal_code
obj.state = geo.state
end
end
def full_street_address
self.street + self.city + self.country
end
end
- /model/address/with_address.rb
class Address::WithAddress < Address
after_validation :geocode
end
- /model/address/with_lat_lng.rb
class Address::WithLatLng < Address
after_validation :reverse_geocode
end
控制器
address = Address::WithLatLng.create(latitude: 40.5236803, longitude: -80.2249546)
# returns: will save lat/lng and address/city/state/zip fields saved from api
address.full_street_address
#or
address = Address::WithAddress.create(address: "601 fake st.", city: "brooklyn", state: "ny", zip: "11220")
# returns: will save address and latitude:, longitude: will fetch from api.
address.full_street_address