Skip to content

AO3-6531 Fix comment decorator 500 error for orphaned comments#5719

Open
pmonfort wants to merge 2 commits intootwcode:masterfrom
pmonfort:AO3-6531-fix-comment-decorator-500
Open

AO3-6531 Fix comment decorator 500 error for orphaned comments#5719
pmonfort wants to merge 2 commits intootwcode:masterfrom
pmonfort:AO3-6531-fix-comment-decorator-500

Conversation

@pmonfort
Copy link
Copy Markdown

@pmonfort pmonfort commented Apr 10, 2026

Pull Request Checklist

Issue

https://otwarchive.atlassian.net/browse/AO3-6531

Purpose

Fixes a 500 error in CommentDecorator.from_thread_ids caused by orphaned comments — replies whose parent comment was physically removed from
the database (via .delete) rather than soft-deleted.

When building the comment tree, the decorator looks up each reply's parent in a hash by commentable_id. If the parent was physically deleted,
the lookup returns nil, causing a NoMethodError. The fix adds a nil check so that orphaned replies are silently skipped when building the
tree, rather than crashing the page.

Additionally, this PR includes a rake task After:create_placeholders_for_orphaned_comments that finds all orphaned comments in the
database and creates soft-deleted placeholder comments in their place. The task:

  • Uses a LEFT JOIN to efficiently find comments whose parent no longer exists
  • Processes orphans top-down using the nested set (threaded_left) to handle cases where multiple comments were deleted in the same branch
  • Creates each placeholder as a child of the closest existing ancestor, preserving the tree structure

Testing Instructions

  1. Create a comment thread with at least 3 levels (root → reply → nested reply)
  2. Physically delete the middle comment from the database (e.g., Comment.find(id).delete)
  3. Visit the page that displays the thread
  4. Verify no 500 error occurs and the root comment still displays
  5. Run the rake task: bundle exec rake After:create_placeholders_for_orphaned_comments
  6. Verify a soft-deleted placeholder was created for the deleted comment
  7. Visit the thread again and verify the placeholder appears as "[deleted comment]"

Credit

Pablo Monfort (he/him)

@pmonfort pmonfort force-pushed the AO3-6531-fix-comment-decorator-500 branch from f37f8de to a6b8fe4 Compare April 10, 2026 02:38
@brianjaustin brianjaustin self-requested a review April 11, 2026 20:05
@pmonfort pmonfort force-pushed the AO3-6531-fix-comment-decorator-500 branch 3 times, most recently from 3fb5cde to 50c3d3e Compare April 12, 2026 19:27
@pmonfort pmonfort force-pushed the AO3-6531-fix-comment-decorator-500 branch from 50c3d3e to c4f3b3f Compare April 13, 2026 00:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant