demarches-normaliennes/db/migrate/20170215102943_remove_duplicate_email_received.rb

12 lines
354 B
Ruby
Raw Normal View History

2017-02-15 15:26:24 +01:00
class RemoveDuplicateEmailReceived < ActiveRecord::Migration[5.0]
def change
2017-08-28 11:18:38 +02:00
all_mails = MailReceived.all
2017-02-15 15:26:24 +01:00
groupped = all_mails.group_by { |m| m.procedure_id }
filtered = groupped.reject { |k, v| v.length < 2 }
filtered.each do |k, duplicate_mails|
duplicate_mails.pop
duplicate_mails.each(&:destroy)
end
end
end