FIX: Failed to delete post belonging to non-existent topic

FIX: Failed to delete post belonging to non-existent topic

From 2ef16e9f4e5d018428ec42d8c0022df1146e8e9d Mon Sep 17 00:00:00 2001
From: Gerhard Schlager <mail@gerhard-schlager.at>
Date: Thu, 22 Nov 2018 15:08:37 +0100
Subject: [PATCH] FIX: Failed to delete post belonging to non-existent topic


diff --git a/app/serializers/post_serializer.rb b/app/serializers/post_serializer.rb
index f778c80..42686fa 100644
--- a/app/serializers/post_serializer.rb
+++ b/app/serializers/post_serializer.rb
@@ -394,7 +394,7 @@ class PostSerializer < BasicPostSerializer
 
   def topic
     @topic = object.topic
-    @topic ||= Topic.with_deleted.find(object.topic_id) if scope.is_staff?
+    @topic ||= Topic.with_deleted.find_by(id: object.topic_id) if scope.is_staff?
     @topic
   end
 
diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb
index 5702bf6..7a91c3b 100644
--- a/spec/components/post_destroyer_spec.rb
+++ b/spec/components/post_destroyer_spec.rb
@@ -566,6 +566,16 @@ describe PostDestroyer do
     end
   end
 
+  it "deletes a post belonging to a non-existent topic" do
+    DB.exec("DELETE FROM topics WHERE id = ?", post.topic_id)
+    post.reload
+
+    PostDestroyer.new(admin, post).destroy
+
+    expect(post.deleted_at).to be_present
+    expect(post.deleted_by).to eq(admin)
+  end
+
   describe 'after delete' do
 
     let!(:coding_horror) { Fabricate(:coding_horror) }

GitHub

2 Likes

This commit has been mentioned on Discourse Meta. There might be relevant details there: