Find duplicated users using Ruby

108 views Asked by At

I am having trouble finding a way to find from a single query if there are users duplicated in my database.

How could I query to find dupped users in Ruby using ActiveRecord?

The database structure is here: http://www.collectivestep.com/discoursedb/

2

There are 2 answers

2
rick On BEST ANSWER

Take an example like you want to find user that have same name and email then try this one and you will get all user that have same name and email and once you get this output delete those User,

User.find(:all, :group => :username, :having => "count(*) > 1" )
0
Rokibul Hasan On

For MySql database you can try following query

User.find(:all, :group => [:username], :having => "count(*) > 1" )

For Postgres you can try following query

User.select(:username).group(:username).having("count(*) > 1")