-
-
Save harssh/f8cff243ce23592ec31a9a5b386da882 to your computer and use it in GitHub Desktop.
remove duplicates from activerecord
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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