Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5 ActiveRecord Delete Duplicates

I have a Rails 5 app. I have a table filled with URL data that is pulled in from various sources:

id     url
1      http://google.com
2      http://yahoo.com
3      http://msn.com
4      http://google.com
5      http://yahoo.com
6      http://askjeeves.com

How can I remove the duplicates from this table?

like image 941
user3063045 Avatar asked Nov 08 '16 03:11

user3063045


Video Answer


2 Answers

SQL solution without loops:

Model.where.not(id: Model.group(:url).select("min(id)")).destroy_all

OR

Model.where.not(id: Model.group(:url).select("min(id)")).delete_all

OR

dup_ids = Model.group(:url).select("min(id)").collect{|m| m['min(id)']}
Model.where.not(id: dup_ids).delete_all
#Model.where.not(id: dup_ids).destroy_all 

This will delete all duplicates keeping records with minimum id for duplicate records.

like image 85
dnsh Avatar answered Nov 15 '22 22:11

dnsh


You can group by url, leave one and delete duplicates:

Model.all.group(:url).values.each do |dup|
  dup.pop #leave one
  dup.each(&:destroy) #destroy other
end
like image 43
idej Avatar answered Nov 15 '22 23:11

idej