← Back to Upcase

Delete records with the value in column


(charlieanna) #1

I have defined a User model with column name to be unique. I have added index to it now. But now I want to remove all the records which has the same name.

How can I delete all the users with the same name?

Thanks


(charlieanna) #2

This worked.

t = User.all.group_by{|model| model.name}
t.values.each do |duplicates|
  first_one = duplicates.shift
  duplicates.each{|double| double.destroy}
end