Skip to content

Instantly share code, notes, and snippets.

@rocLv
Forked from davinmsu/company.rb
Created April 18, 2017 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rocLv/e432e6814cee4d2d1b727e53e9e5419b to your computer and use it in GitHub Desktop.
Save rocLv/e432e6814cee4d2d1b727e53e9e5419b 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