A platform for community discussion. Free, open, simple.
FIX: Avoid exception when rendering a poll in a trashed post
David Taylor84be92c0671b
diff --git a/plugins/poll/app/models/poll.rb b/plugins/poll/app/models/poll.rbindex 4f7e0bd584a..44ce17f46cf 100644--- a/plugins/poll/app/models/poll.rb+++ b/plugins/poll/app/models/poll.rb@@ -4,7 +4,7 @@ class Poll < ActiveRecord::Base# because we want to use the 'type' column and don't want to use STIself.inheritance_column = nil- belongs_to :post+ belongs_to :post, -> { unscope(:where) }has_many :poll_options, -> { order(:id) }, dependent: :destroyhas_many :poll_votes@@ -51,7 +51,7 @@ class Poll < ActiveRecord::Baseenddef is_me?(user)- user && post.user&.id == user&.id+ user && post && post.user&.id == user&.idenddef has_voted?(user)diff --git a/plugins/poll/spec/models/poll_spec.rb b/plugins/poll/spec/models/poll_spec.rbindex 55e6567d47c..fed3310b84a 100644--- a/plugins/poll/spec/models/poll_spec.rb+++ b/plugins/poll/spec/models/poll_spec.rb@@ -69,4 +69,18 @@ describe ::DiscoursePoll::Poll doexpect(poll.can_see_results?(user)).to eq(true)endend++ describe 'when post is trashed' do+ it "maintains the association" do+ user = Fabricate(:user)+ post = Fabricate(:post, raw: "[poll results=staff_only]\n- A\n- B\n[/poll]", user: user)+ poll = post.polls.first++ post.trash!+ poll.reload++ expect(poll.post).to eq(post)+ end++ endend