Skip to content

Instantly share code, notes, and snippets.

@ocxo
Created April 11, 2011 13:02
Show Gist options
  • Save ocxo/913478 to your computer and use it in GitHub Desktop.
Save ocxo/913478 to your computer and use it in GitHub Desktop.
class Location
include Mongoid::Document
include Geocoder::Model::Mongoid
field :user_defined_location
field :lat, :type => Float
field :lon, :type => Float
field :coords, :type => Array
index :coords
...
geocoded_by :user_defined_location, :coordinates => :coords, :latitude => :lat, :longitude => :lon
reverse_geocoded_by :coords do |location, results|
if geo = results.first
location.lat = geo.latitude
location.lon = geo.longitude
location.coords = geo.coords
end
end
after_validation :geocode, :reverse_geocode,
:if => lambda{ |obj| obj.user_defined_location_changed? }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment