diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index e31b08fbe9..7954e3b86a 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -869,7 +869,7 @@ class PostsController < ApplicationController post = finder.with_deleted.first raise Discourse::NotFound unless post - post.topic = Topic.with_deleted.find(post.topic_id) + post.topic = Topic.with_deleted.find_by(id: post.topic_id) if !post.topic || ( diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb index f0cc6654bb..96a6ceb78a 100644 --- a/spec/requests/posts_controller_spec.rb +++ b/spec/requests/posts_controller_spec.rb @@ -16,6 +16,12 @@ shared_examples 'finding and showing post' do expect(response.status).to eq(200) end + it "returns 404 when post's topic is deleted" do + post.topic.destroy! + get url + expect(response.status).to eq(404) + end + context "deleted post" do before do post.trash!(user)