distance_by method is not working correctly
pblogs opened this issue · comments
So, I'm using geokit-rails and I would like to sort by distance, specifically showing the nearest meals first and descending from there. e.g. 5km, 10km, 15km.
However, for some reason the distance_by method isn't working at all, and the distances are all over the place.
This is how we're using it:
all_venues = Venue.where( 'listed = ? AND packages_count > ?', true, 0 ).by_distance(origin: search_location).search(params[:search]) || Venue.where(listed: true).all
Are there any work around methods to use? Or how can I sort from nearest to furthest with geokit-rails?
Thanks!
+1
Can confirm this is an issue for me as well. The SQL that is generated in for my use case (in postgres) is:
Place.by_distance(origin: [51.672607, 0.651835]).to_sql
#=> SELECT "places".* FROM "places" WHERE (places.latitude IS NOT NULL AND places.longitude IS NOT NULL) ORDER BY "places"."name" ASC, (ACOS(least(1,COS(0.9018571252390695)*COS(0.01137666692973724)*COS(RADIANS(places.latitude))*COS(RADIANS(places.longitude))+ COS(0.9018571252390695)*SIN(0.01137666692973724)*COS(RADIANS(places.latitude))*SIN(RADIANS(places.longitude))+ SIN(0.9018571252390695)*SIN(RADIANS(places.latitude))))*3963.1899999999996) ASC
Looking under the hood, Postgres ignores the distance based ordering. If I add it to the select query as a virtual column, like so:
SELECT "places".*, (ACOS(least(1,COS(0.9018571252390695)*COS(0.01137666692973724)*COS(RADIANS(places.latitude))*COS(RADIANS(places.longitude))+ COS(0.9018571252390695)*SIN(0.01137666692973724)*COS(RADIANS(places.latitude))*SIN(RADIANS(places.longitude))+ SIN(0.9018571252390695)*SIN(RADIANS(places.latitude))))*3963.1899999999996) AS distance FROM "places" WHERE (places.latitude IS NOT NULL AND places.longitude IS NOT NULL) ORDER BY distance
I get the expected result. I'm using Postgres 9.6.5 and Rails 4.2 BTW.