namespace :topics do
  desc "Map topics assigned via policies directly onto editions"
  task from_policies_to_editions: :environment do

    class TopicTransfer < Struct.new(:logger, :edition)
      def copy_topics_from_policies
        existing_topics = edition.directly_associated_topics.pluck(:id)
        new_topics = policy_topics.select do |topic|
          ! existing_topics.include?(topic.id)
        end
        logger.info "Copying #{new_topics.size} topics to #{edition.slug}"
        edition.directly_associated_topics << new_topics
      end

      def policy_topics
        edition.related_policies.map(&:topics).flatten.uniq
      end
    end

    logger = Logger.new($stderr)

    [Announcement, Publicationesque].each do |edition_type|
      logger.info "-- Associating #{edition_type.name.downcase.pluralize} directly with policies' topics"
      edition_type.includes(:policy_topics).find_each do |announcement|
        TopicTransfer.new(logger, announcement).copy_topics_from_policies
      end
    end
  end
end

namespace :topics do
  desc "Map topics assigned via policies directly onto editions"
  task from_policies_to_editions: :environment do

    class TopicTransfer < Struct.new(:logger, :edition)
      def copy_topics_from_policies
        existing_topics = edition.directly_associated_topics.pluck(:id)
        new_topics = policy_topics.select do |topic|
          ! existing_topics.include?(topic.id)
        end
        logger.info "Copying #{new_topics.size} topics to #{edition.slug}"
        edition.directly_associated_topics << new_topics
      end

      def policy_topics
        edition.related_policies.map(&:topics).flatten.uniq
      end
    end

    logger = Logger.new($stderr)

    [Announcement, Publicationesque].each do |edition_type|
      logger.info "-- Associating #{edition_type.name.downcase.pluralize} directly with policies' topics"
      edition_type.find_each do |announcement|
        TopicTransfer.new(logger, announcement).copy_topics_from_policies
      end
    end
  end
end

Remove includes as it's buggy
Remove includes as it's buggy

@heathd and I have dug into this more - when using `includes(:policy_topics)`
the `class_name` directive for the association isn't used, which causes this to
load WorldwidePriority objects instead of the Policy objects we actually
wanted.

Removing this will make the query slower but won't hit this bug, which
is arguably a bug in ActiveRecord.

The Edition which triggers this the first instance of this error is 160804.
