Created
February 3, 2014 17:34
-
-
Save davinmsu/8788377 to your computer and use it in GitHub Desktop.
remove duplicates from activerecord
This file contains 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
how do I club order_by to the above group_by?