Cleanup invalid polls without status message

closes #7614
parent 00296ffd
......@@ -20,6 +20,7 @@
* Prevent users from zooming in IE Mobile [#7589](
* Fix recipient prefill on contacts and profile page [#7599](
* Display likes and reshares without login [#7583](
* Fix invalid data in the database for user data export [#7614](
## Features
* Ask for confirmation when leaving a submittable comment field [#7530](
......@@ -5,8 +5,8 @@ class Poll < ApplicationRecord
include Diaspora::Fields::Guid
belongs_to :status_message
has_many :poll_answers, -> { order 'id ASC' }
has_many :poll_participations
has_many :poll_answers, -> { order "id ASC" }, dependent: :destroy
has_many :poll_participations, dependent: :destroy
has_one :author, through: :status_message
#forward some requests to status message, because a poll is just attached to a status message and is not sharable itself
......@@ -20,7 +20,7 @@ class StatusMessage < Post
has_many :photos, :dependent => :destroy, :foreign_key => :status_message_guid, :primary_key => :guid
has_one :location
has_one :poll, autosave: true
has_one :poll, autosave: true, dependent: :destroy
has_many :poll_participations, through: :poll
attr_accessor :oembed_url
class CleanupInvalidPolls < ActiveRecord::Migration[5.1]
class Poll < ApplicationRecord
has_many :poll_answers, dependent: :destroy
has_many :poll_participations, dependent: :destroy
class PollAnswer < ApplicationRecord
belongs_to :poll
has_many :poll_participations
class PollParticipation < ApplicationRecord
belongs_to :poll
belongs_to :poll_answer
def up
Poll.joins("LEFT OUTER JOIN posts ON = polls.status_message_id")
.where(" IS NULL").destroy_all
......@@ -233,6 +233,23 @@ describe StatusMessage, type: :model do
describe "poll" do
it "destroys the poll (with all answers and participations) when the status message is destroyed" do
poll = FactoryGirl.create(:poll_participation).poll
status_message = poll.status_message
poll_id =
poll_answers =
poll_participations =
expect(Poll.where(id: poll_id)).not_to exist
poll_answers.each {|id| expect(PollAnswer.where(id: id)).not_to exist }
poll_participations.each {|id| expect(PollParticipation.where(id: id)).not_to exist }
describe "validation" do
let(:status_message) { build(:status_message, text: @message_text) }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment