Skip to content

Instantly share code, notes, and snippets.

@harssh
Forked from davinmsu/company.rb
Created November 3, 2022 16:02
Show Gist options
  • Save harssh/f8cff243ce23592ec31a9a5b386da882 to your computer and use it in GitHub Desktop.
Save harssh/f8cff243ce23592ec31a9a5b386da882 to your computer and use it in GitHub Desktop.
remove duplicates from activerecord
def self.dedupe
# find all models and group them on keys which should be common
grouped = all.group_by{|model| [model.title,model.info,model.address,model.phone] }
grouped.values.each do |duplicates|
# the first one we want to keep right?
first_one = duplicates.shift # or pop for last one
# if there are any more left, they are duplicates
# so delete all of them
duplicates.each{|double| double.destroy} # duplicates can now be destroyed
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment