Rails|Ransack|Geocoder – Sort data by distance

If your application connects local businesses to local customer, then you might have used Ransack for sorting and searching of records. For geocoding and reverse-geocoding, you might be using Geocoder gem. If you want to let users to sort results by distance then its pretty easy.

Things to remember,

  • If you use Model.near([lat, long]) method, Ransack adds a new column called distance to the ActiverecordRelation object. You can access the method distance by prying.

Continue reading

Advertisements

Rails : Ransack : Sorting data by ratings

Its not quite easily graspable to sort your search results by Ratings if you have separate Ratings table and there is an intermediate table called Job or Similar. Example Model schema is below for reference.

Continue reading