Skip to content

Instantly share code, notes, and snippets.

@mrjonesbot
Created December 12, 2018 12:58
Show Gist options
  • Save mrjonesbot/3935b77c31f7cfb7739829c1b320e010 to your computer and use it in GitHub Desktop.
Save mrjonesbot/3935b77c31f7cfb7739829c1b320e010 to your computer and use it in GitHub Desktop.
namespace :resource do
desc 'remove duplicate saved students'
task cleanup: :environment do
# find all models and group them on keys which should be common
grouped_by_foreign_keys = SavedStudent.all.group_by do |model|
[model.recruiter_user_id, model.student_profile_id]
end
# grouped_by_email = Person.all.group_by{|model| [model.email] }
# grouped_by_foreign_keys.merge(grouped_by_email).values.each do |duplicates|
grouped_by_foreign_keys.values.each do |duplicates|
# duplicates.shift # -> keep the first one
duplicates.pop # -> keep the last one
# if there are any more left, they are duplicates
# so delete all of them
duplicates.each(&:destroy) # duplicates can now be destroyed
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment