title: How to find records that have any duplicate data using Active Record
body: <p>How to find records with duplicate values in any column using Activerecord or SQL?</p>
  
  <pre><code>SELECT leads.id, leads.name, leads.email, leads.created_at, array_agg(tn2.id) as ids
  FROM "leads" join leads tn2
      on leads.name = tn2.name
        or leads.cpf_cnpj = tn2.cpf_cnpj
        or leads.email = tn2.email
        or leads.phone -&gt; 'cellphone' = tn2.phone -&gt; 'cellphone'
        or leads.phone -&gt; 'residence' = tn2.phone -&gt; 'residence'
        or leads.phone -&gt; 'commercial' = tn2.phone -&gt; 'commercial' 
  GROUP BY leads.id  ORDER BY leads.created_at DESC
  </code></pre>
  
  <p>Using <code>array_agg</code> I want only ids from repeated objects, but it gives me from all records. 
  Currently, I'm using PostgreSQL.</p>