title: Rails GROUP BY, COUNT
body: <p>I have a Tag table, that looks like this:</p>
  
  <pre><code>NAME  | USER_ID |  OBJECT_ID 
  ---------------------------
  "fun" | 1       | 1
  "fun" | 2       | 1
  </code></pre>
  
  <p>Object is a taggable object, nothing more than that. Now, the problem I'm experiencing is:
  I want to display non-duplicate tags for the object, but with ability to say which user each tag belongs to.</p>
  
  <p>So, I have declared following association for the taggable object: </p>
  
  <blockquote>
    <p>has_many :tags, :group => "name", :order => "count(<code>tags</code>.<code>name</code>)
    DESC, <code>tags</code>.<code>name</code> ASC"</p>
  </blockquote>
  
  <p>So, it does two things: 1) groups by tag name -> eliminates the duplicates 2) orders the tags by the number of records</p>
  
  <p>But, at the same time I need to determine if the visitor on the page (logged-in user) has left any tags or not, and it is not possible, because GROUP selects random tag from the list and only one user can be determined.</p>
  
  <p>How would you solve this issue ?</p>