title: Rails and ActiveRecord - Nearest points by latitude and longitude
body: <p>I am currently porting a PHP application to Rails. I need to calculate nearest venues from a given latitude and longitude. </p>
  
  <p>In my PHP application, I had this MySQL query (clubs are the venues I'm talking about):</p>
  
  <pre><code>SELECT cl.*, c.id, c.slug AS city_slug, 
  (3959 * acos(cos(radians(:lat)) * cos(radians(cl.lat)) * cos(radians(cl.lng) - radians(:lng)) + sin(radians(:lat)) * sin(radians(cl.lat)))) 
  AS distance 
  FROM club cl, city c
  WHERE cl.published = 1
  AND c.id = cl.city_id
  AND cl.id &lt;&gt; :club_id
  AND cl.deleted = 0
  ORDER BY distance 
  LIMIT 0,10  
  </code></pre>
  
  <p>As you can see, the calculations are being made in the database layer and I would like it to stay like that. </p>
  
  <p>How can I achieve this with Rails, ActiveRecord and PostgreSQL?</p>