You are asking a lot that has to be cut into small pieces.
Querying a SQL database is best done by ActiveRecord, included with Rails. Using location with that is easily achieved with a gem such as Geocoder. Sorting and ranking by your own algorithm should by done by yourself: it is you own algorithm. However, you could use the supplied .order / .sort methods of ActiveRecord.