I'm using the geocoder gem for Ruby that Ryan Bates introduces in episode 273 http://railscasts.com/episodes/273-geocoder?view=asciicast. In that episode, he uses the following to search objects within a 50 km radius (although he uses a Location model, where I use a User model).
@users = User.near(params[:search], 50, :order => :distance)
when I run this code on a Rails 4 app with postgres, it says
PG::Error: ERROR: column users.distance does not exist
In Ryan's demo app (code here https://github.com/railscasts/273-geocoder/tree/master/siteseer-after) he doesn't have a distance column on his Location model (as I don't have it on my user model) so I assumed that distance
was something supplied by the Geocoder gem. However, looking at the docs for geocoder http://rdoc.info/github/alexreisner/geocoder/master/frames, there is no distance
method and all of the other distance-like methods produce the same error.
Does anyone know how I'd order results in that query by distance?
Update
On the geocoder homepage http://www.rubygeocoder.com/ it gives this example of the use of distance
, however when I try it it says that it doesn't exist for user.
nearbys = Place.near("Omaha, NE", 50,
:order => "distance")
bearing = nearbys.first.bearing # => 46.12
Geocoder::Calculations.compass_point(
bearing) # => "NE"
Update
This is the sql generated
ActionView::Template::Error (PG::Error: ERROR: column users.distance does not exist
LINE 1: ...ngitude) * PI() / 180 / 2), 2))) <= 50) ORDER BY "users".di...
^
: SELECT users.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((43.7058645 - users.latitude) * PI() / 180 / 2), 2) + COS(43.7058645 * PI() / 180) * COS(users.latitude * PI() / 180) * POWER(SIN((-79.3687137 - users.longitude) * PI() / 180 / 2), 2))) AS distance, CAST(DEGREES(ATAN2( RADIANS(users.longitude - -79.3687137), RADIANS(users.latitude - 43.7058645))) + 360 AS decimal) % 360 AS bearing FROM "users" WHERE (users.latitude BETWEEN 42.98220558444576 AND 44.429523415554236 AND users.longitude BETWEEN -80.36976846878524 AND -78.36765893121476 AND 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((43.7058645 - users.latitude) * PI() / 180 / 2), 2) + COS(43.7058645 * PI() / 180) * COS(users.latitude * PI() / 180) * POWER(SIN((-79.3687137 - users.longitude) * PI() / 180 / 2), 2))) <= 50) ORDER BY "users".distance ASC):
ORDER BY distance
– Felixfeliza