title: How to do this group ActiveRecord query in postgres
body: <p>I try to find over a 3M table, all the users who have the same username. I read something like this may do the trick. </p>
  
  <pre><code>User.find(:all, :group =&gt; [:username], :having =&gt; "count(*) &gt; 1" )
  </code></pre>
  
  <p>However since I'm using Postgres this return me <code>ActiveRecord::StatementInvalid: PG::Error: ERROR:  column "users.id" must appear in the GROUP BY clause or be used in an aggregate function</code>. </p>
  
  <p>I'm trying something like this</p>
  
  <pre><code>User.select('users.id, users.username').having("count(*) &gt; 1").group('users.username')
  </code></pre>
  
  <p>But still get the same error. Any idea what I'm doing wrong?</p>
  
  <p><strong>Update:</strong> I made it somehow work using <code>User.select('users.*').group('users.id').having('count(users.username) &gt; 1')</code> but this query returns me this which looks like an empty array even if is founding 5 records.</p>
  
  <pre><code> GroupAggregate  (cost=9781143.40..9843673.60 rows=3126510 width=1365)
     Filter: (count(username) &gt; 1)
     -&gt;  Sort  (cost=9781143.40..9788959.68 rows=3126510 width=1365)
           Sort Key: id
           -&gt;  Seq Scan on users  (cost=0.00..146751.10 rows=3126510 width=1365)
  (5 rows)
  
   =&gt; [] 
  </code></pre>
  
  <p>Any idea why this is happening and how to get those 5 rows?</p>