Skip to content

Instantly share code, notes, and snippets.

@poudelmadhav
Last active April 14, 2020 12:05
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 poudelmadhav/80fcd3c9eb9fec6c7983af8fdee20f18 to your computer and use it in GitHub Desktop.
Save poudelmadhav/80fcd3c9eb9fec6c7983af8fdee20f18 to your computer and use it in GitHub Desktop.
removing duplicates from rails model
models = Model.all.group_by{|model| [model.attr1,model.attr2,model.attr3] }; nil
models.values.each do |duplicates|
  first_one = duplicates.shift
  duplicates.each{|double| double.destroy} # duplicates can now be destroyed
end

Source: Remove duplicate records based on multiple columns?

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