title: Adding custom SELECT in ActiveRecord 3
body: <p>I am trying to use the result of a custom query (a distance between two geographical points) as a model attribute. The end goal is to be able to filter and order rows using that column, e.g. :</p>
  
  <pre><code>Model.with_distance(point).order(:distance).first
  </code></pre>
  
  <p>I initially thought I could use <code>select</code> in a scope ; here is a shorter version without sanitization :</p>
  
  <pre><code>scope :with_distance, -&gt;(point) { select("ST_DISTANCE(models.point_in_model, ST_GeographyFromText('#{point.to_s}')) AS distance") }
  </code></pre>
  
  <p>However, that scope only selects the <code>distance</code> column, without retrieving model attributes :</p>
  
  <pre><code>&gt; Model.with_distance(point)
  [#&lt;Model &gt;, #&lt;Model &gt;,...]
  </code></pre>
  
  <p>Adding <code>models.*</code> to the <code>select</code> call solves the problem, at the cost of forcing the retrieval of the entire row in each query and ignoring other <code>select</code> calls.</p>
  
  <p>How can I <strong>add a SELECT clause</strong> instead of <strong>replacing all of them</strong>?</p>