diff --git a/app/models/post_mover.rb b/app/models/post_mover.rb index 845226959..fdd972322 100644 --- a/app/models/post_mover.rb +++ b/app/models/post_mover.rb @@ -78,7 +78,7 @@ class PostMover end PostReply.where("reply_id in (:post_ids) OR post_id in (:post_ids)", post_ids: post_ids).each do |post_reply| - if post_reply.reply.topic_id != post_reply.post.topic_id + if post_reply.post && post_reply.reply && post_reply.reply.topic_id != post_reply.post.topic_id PostReply.delete_all(reply_id: post_reply.reply.id, post_id: post_reply.post.id) end end diff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rb index 03450fe05..1a8077804 100644 --- a/spec/models/post_mover_spec.rb +++ b/spec/models/post_mover_spec.rb @@ -80,7 +80,18 @@ describe PostMover do topic.move_posts( user, [p2.id], - title: 'GOT is a very addictive showw', category_id: category.id + title: 'GOT is a very addictive show', category_id: category.id + ) + + expect(p2.reload.replies).to eq([]) + end + + it "doesn't raise errors with deleted replies" do + p4.trash! + topic.move_posts( + user, + [p2.id], + title: 'GOT is a very addictive show', category_id: category.id ) expect(p2.reload.replies).to eq([]) @@ -101,7 +112,7 @@ describe PostMover do topic.move_posts( user, [p4.id], - title: 'GOT is a very addictive showw', category_id: category.id + title: 'GOT is a very addictive show', category_id: category.id ) expect(p2.reload.replies).to eq([p5]) @@ -122,7 +133,7 @@ describe PostMover do topic.move_posts( user, [p2.id, p4.id], - title: 'GOT is a very addictive showw', category_id: category.id + title: 'GOT is a very addictive show', category_id: category.id ) expect(p2.reload.replies).to eq([p4])