title: ActiveRecord query is forcing ORDER BY after DISTINCT
body: <p>I am using this query:</p>
  
  <pre><code>Post.where("created_at &gt;= ? AND created_at &lt;= ?", 1.month.ago.beginning_of_month , 1.month.ago.end_of_month)
      .select('DISTINCT user_id')
  </code></pre>
  
  <p>Which returns 40 elements. However, I expect to get an array from this query, and to be able to iterate over this array with a <code>.each</code>. The issue is that when I try to use <code>.each</code> on the result of the query, I get this:</p>
  
  <blockquote>
    <p>ActiveRecord::StatementInvalid: PG::InvalidColumnReference: ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list
        LINE 1: ...ted_at &lt;= '2016-06-30 21:59:59.999999')  ORDER BY created_at...
                                                                     ^
        : SELECT DISTINCT user_id FROM "posts" WHERE (created_at >= '2016-05-31 22:00:00.000000' AND created_at &lt;= '2016-06-30 21:59:59.999999')  ORDER BY created_at DESC</p>
  </blockquote>
  
  <p>It seems like this query forces a <code>ORDER BY</code> on the <code>created_at</code>, and makes it break since order_by isn't in the query.</p>
  
  <p>To avoid this issue, I'm using: </p>
  
  <pre><code>Post.where("created_at &gt;= ? AND created_at &lt;= ?", 1.month.ago.beginning_of_month , 1.month.ago.end_of_month)
      .map{|p| p.user_id}.uniq
  </code></pre>
  
  <p>Which is less efficient.</p>
  
  <p>Any idea why I'm getting this error? </p>