Clean assign_to duplicates with a rake task

This commit is contained in:
Mathieu Magnin 2017-08-01 11:27:14 +02:00
parent b377d8b538
commit 7fdcc91e0c

View file

@ -0,0 +1,12 @@
namespace :'2017_08_01_clean_assign_to' do
task clean: :environment do
duplicates = AssignTo.group(:gestionnaire_id, :procedure_id).count.select{ |_gestionnaire_id_procedure_id, count| count > 1 }.keys
duplicate_ids = duplicates.map { |gestionnaire_id, procedure_id| AssignTo.where(gestionnaire_id: gestionnaire_id, procedure_id: procedure_id).pluck(:id) }
duplicate_ids.each do |ids|
ids.pop
AssignTo.where(id: ids).destroy_all
end
end
end