title: Rails 3 ActiveRecord: order and group by association count
body: <p>My question is very similar to <a href="https://stackoverflow.com/questions/8696005/rails-3-activerecord-order-by-count-on-association">Rails 3 ActiveRecord: Order by count on association</a></p>
  
  <p>Given the same scenario where the model </p>
  
  <p><code>Song has many :listens</code></p>
  
  <p>I would like to group the songs by the number of listens. My goal is to see a distribution of songs vs listen count. Something like...</p>
  
  <pre><code>song_listen_distribution = {0 =&gt; 24, 1 =&gt; 43, 2=&gt;11, ... MAX_LISTENS =&gt; 1}
  </code></pre>
  
  <p>so that <code>song_listen_distribution[4]</code> would return the number of songs listened to 4 times.</p>
  
  <p>The accepted answer to the linked question above gets me very close, but I am unable to group by "songs.listens_count"</p>
  
  <pre><code>    Song.select("songs.id, OTHER_ATTRS_YOU_NEED, count(listens.id) AS listens_count").
      joins(:listens).
      group("songs.listens_count").
      order("listens_count DESC")
  </code></pre>