Jerome
Jerome

Reputation: 6189

Accessing geocoder gem scopes

The gem manual indicates Venue.near([40.71, -100.23], 20, :units => :km)

However this application's model objects have two geocoded co-ordinates variables in addition to the individual longitudes and latitudes. In order to test the various search options (postGIS, numerical indexes of lon and lat, or calling geocoder services) is there any way to access a specific model variable in the finding of objects near a given location?

The relevant schema data is:

t.decimal  "origin_lon",          precision: 15, scale: 10
t.decimal  "origin_lat",          precision: 15, scale: 10
t.spatial  "origin_lonlat",      limit: {:srid=>3857, :type=>"point"}
add_index "strappos", ["origin_lat"], :name => "index_strappos_on_origin_lat"
add_index "strappos", ["origin_lon"], :name => "index_strappos_on_origin_lon"
add_index "strappos", ["origin_lonlat"], :name => "index_strappos_on_origin_lonlat", :spatial => true

Upvotes: 0

Views: 38

Answers (1)

Wes Foster
Wes Foster

Reputation: 8900

GeoCoder defaults to using latitude and longitude as their variable names. Since yours are different, you'll need to add an extra option in order to query origin_lon and origin_lat.

In your model, where you initialize geocoder for the model, you have the option of assigning a different name for the lat/lon attribute:

# YourModel.rb
geocoded_by :address,
  latitude: :origin_lat, 
  longitude: :origin_lon

I placed it on multiple lines for clarity. As you can see, I'm telling geocoder that my lat/lon attribute names are different than the defaults.

Now, querying the model using near will look at those new attribute names as opposed to the defaults.

Upvotes: 1

Related Questions