How do I order the results of rails geocoder gem by something other than distance
Asked Answered
N

2

8

I have this excellent gem https://github.com/alexreisner/geocoder working fine in my app but I need to order the results of an active record query slightly differently.

For example using the rails console

$> Person.near('London',10)

produces a long SQL query (not relevant) followed by

ORDER BY distance ASC

This gives a nice ordered list of People from closest to furthest (upto 10 miles away). Now if I do this

$> Person.near('London',10).order('price_per_hour')

The query is appended by

ORDER BY distance ASC, price_per_hour

I still want all the people near London but I dont want it ordering by distance. There doesnt seem to be anyway of taking this out.

I can't sort outside of the active record query because I am using pagination so ordering needs to be done in the main query which is then paginated.

Neysa answered 20/12, 2012 at 9:22 Comment(0)
B
17

You should try reorder instead of order :

Person.near('London',10).reorder('price_per_hour')

The reorder removes and replace the order statement created by the scope near.

Boast answered 20/12, 2012 at 9:29 Comment(0)
R
1

I know its too late. Now geocoder gem provides to remove default order of distance

Person.near('London',10,order: false).order('price_per_hour')
Reine answered 6/3, 2018 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.