20170914212336_cleanup_invalid_polls.rb 545 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class CleanupInvalidPolls < ActiveRecord::Migration[5.1]
  class Poll < ApplicationRecord
    has_many :poll_answers, dependent: :destroy
    has_many :poll_participations, dependent: :destroy
  end
  class PollAnswer < ApplicationRecord
    belongs_to :poll
    has_many :poll_participations
  end
  class PollParticipation < ApplicationRecord
    belongs_to :poll
    belongs_to :poll_answer
  end

  def up
    Poll.joins("LEFT OUTER JOIN posts ON posts.id = polls.status_message_id")
        .where("posts.id IS NULL").destroy_all
  end
end