Skip to content

Instantly share code, notes, and snippets.

@davinmsu
Created February 3, 2014 17:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save davinmsu/8788377 to your computer and use it in GitHub Desktop.
Save davinmsu/8788377 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
@SharvariNagesh
Copy link

how do I club order_by to the above group_by?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment