Skip to content

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

Merged
brianjaustin merged 4 commits intootwcode:masterfrom
pmonfort:AO3-6531-fix-comment-decorator-500
Apr 14, 2026
Merged

AO3-6531 Fix comment decorator 500 error for orphaned comments#5719
brianjaustin merged 4 commits intootwcode:masterfrom
pmonfort:AO3-6531-fix-comment-decorator-500

Conversation

@pmonfort
Copy link
Copy Markdown
Contributor

@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
Copy link
Copy Markdown
Member

@brianjaustin brianjaustin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one naming thing, otherwise looking good! (I've approved already because I don't think a small nitpick is blocking either way)

Comment thread lib/tasks/after_tasks.rake Outdated
@pmonfort pmonfort force-pushed the AO3-6531-fix-comment-decorator-500 branch from 6645ea2 to 9d458c5 Compare April 13, 2026 12:49
@brianjaustin brianjaustin merged commit 9e7932b into otwcode:master Apr 14, 2026
29 checks passed
jmalark pushed a commit to jmalark/otwarchive that referenced this pull request Apr 19, 2026
…de#5719)

* AO3-6531 Fix comment decorator 500 error when parent comment is physically deleted

* AO3-6531 Add rake task to create placeholders for orphaned comments

* AO3-6531 Rename orphaned_commentable_ids variable to missing_commentable_ids.Suggested by brianjaustin.
jmalark added a commit to jmalark/otwarchive that referenced this pull request Apr 19, 2026
AO3-6229 Add static 404 error page (otwcode#5711)

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

* AO3-6531 Fix comment decorator 500 error when parent comment is physically deleted

* AO3-6531 Add rake task to create placeholders for orphaned comments

* AO3-6531 Rename orphaned_commentable_ids variable to missing_commentable_ids.Suggested by brianjaustin.

AO3-7378 Updates for code review

AO3-6531 Fix create_placeholders_for_orphaned_comments task 500 (otwcode#5736)

* AO3-6531 - Add spec for orphaned comment with nil threaded_left

* AO3-6531 - Handle nil threaded_left in orphaned comment placeholder task

AO3-7381 Update Devise to 5.0.3 (otwcode#5727)

AO3-6531 Remove after task for placeholder comments (otwcode#5740)

Per comments on the related ticket, this could lead to weird behaviour.
Pulling this out so the fix for the most critical part can go live.

AO3-7080 Fix suspended and banned user check (otwcode#5741)

AO3-7378 add double trailing whitespace to try to get it to let me push whitespace changes after settings changes

AO3-7378 Add trailing new line

AO3-7378 Get my local back to just one line of trailing whitespace

AO3-7378 Still trying to get trailing whitespace settings to cooperate

AO3-7378 Trailing whitespace adjustment

AO3-6229 Add static 404 error page (otwcode#5711)

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

* AO3-6531 Fix comment decorator 500 error when parent comment is physically deleted

* AO3-6531 Add rake task to create placeholders for orphaned comments

* AO3-6531 Rename orphaned_commentable_ids variable to missing_commentable_ids.Suggested by brianjaustin.

AO3-7378 Updates for code review

AO3-6531 Fix create_placeholders_for_orphaned_comments task 500 (otwcode#5736)

* AO3-6531 - Add spec for orphaned comment with nil threaded_left

* AO3-6531 - Handle nil threaded_left in orphaned comment placeholder task

AO3-7381 Update Devise to 5.0.3 (otwcode#5727)

AO3-6531 Remove after task for placeholder comments (otwcode#5740)

Per comments on the related ticket, this could lead to weird behaviour.
Pulling this out so the fix for the most critical part can go live.

AO3-7080 Fix suspended and banned user check (otwcode#5741)

AO3-7378 add double trailing whitespace to try to get it to let me push whitespace changes after settings changes

AO3-7378 Add trailing new line

AO3-7378 Get my local back to just one line of trailing whitespace

AO3-7378 Still trying to get trailing whitespace settings to cooperate

AO3-7378 Trailing whitespace adjustment
jmalark added a commit to jmalark/otwarchive that referenced this pull request Apr 19, 2026
AO3-6229 Add static 404 error page (otwcode#5711)

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

* AO3-6531 Fix comment decorator 500 error when parent comment is physically deleted

* AO3-6531 Add rake task to create placeholders for orphaned comments

* AO3-6531 Rename orphaned_commentable_ids variable to missing_commentable_ids.Suggested by brianjaustin.

AO3-7378 Updates for code review

AO3-6531 Fix create_placeholders_for_orphaned_comments task 500 (otwcode#5736)

* AO3-6531 - Add spec for orphaned comment with nil threaded_left

* AO3-6531 - Handle nil threaded_left in orphaned comment placeholder task

AO3-7381 Update Devise to 5.0.3 (otwcode#5727)

AO3-6531 Remove after task for placeholder comments (otwcode#5740)

Per comments on the related ticket, this could lead to weird behaviour.
Pulling this out so the fix for the most critical part can go live.

AO3-7080 Fix suspended and banned user check (otwcode#5741)

AO3-7378 add double trailing whitespace to try to get it to let me push whitespace changes after settings changes

AO3-7378 Add trailing new line

AO3-7378 Get my local back to just one line of trailing whitespace

AO3-7378 Still trying to get trailing whitespace settings to cooperate

AO3-7378 Trailing whitespace adjustment

AO3-6229 Add static 404 error page (otwcode#5711)

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

* AO3-6531 Fix comment decorator 500 error when parent comment is physically deleted

* AO3-6531 Add rake task to create placeholders for orphaned comments

* AO3-6531 Rename orphaned_commentable_ids variable to missing_commentable_ids.Suggested by brianjaustin.

AO3-7378 Updates for code review

AO3-6531 Fix create_placeholders_for_orphaned_comments task 500 (otwcode#5736)

* AO3-6531 - Add spec for orphaned comment with nil threaded_left

* AO3-6531 - Handle nil threaded_left in orphaned comment placeholder task

AO3-7381 Update Devise to 5.0.3 (otwcode#5727)

AO3-6531 Remove after task for placeholder comments (otwcode#5740)

Per comments on the related ticket, this could lead to weird behaviour.
Pulling this out so the fix for the most critical part can go live.

AO3-7080 Fix suspended and banned user check (otwcode#5741)

AO3-7378 add double trailing whitespace to try to get it to let me push whitespace changes after settings changes

AO3-7378 Add trailing new line

AO3-7378 Get my local back to just one line of trailing whitespace

AO3-7378 Still trying to get trailing whitespace settings to cooperate

AO3-7378 Trailing whitespace adjustment

AO3-6229 Add static 404 error page (otwcode#5711)

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

* AO3-6531 Fix comment decorator 500 error when parent comment is physically deleted

* AO3-6531 Add rake task to create placeholders for orphaned comments

* AO3-6531 Rename orphaned_commentable_ids variable to missing_commentable_ids.Suggested by brianjaustin.

AO3-7378 Updates for code review

AO3-6531 Fix create_placeholders_for_orphaned_comments task 500 (otwcode#5736)

* AO3-6531 - Add spec for orphaned comment with nil threaded_left

* AO3-6531 - Handle nil threaded_left in orphaned comment placeholder task

AO3-7381 Update Devise to 5.0.3 (otwcode#5727)

AO3-6531 Remove after task for placeholder comments (otwcode#5740)

Per comments on the related ticket, this could lead to weird behaviour.
Pulling this out so the fix for the most critical part can go live.

AO3-7080 Fix suspended and banned user check (otwcode#5741)

AO3-7378 add double trailing whitespace to try to get it to let me push whitespace changes after settings changes

AO3-7378 Add trailing new line

AO3-7378 Get my local back to just one line of trailing whitespace

AO3-7378 Still trying to get trailing whitespace settings to cooperate

AO3-7378 Trailing whitespace adjustment

AO3-6229 Add static 404 error page (otwcode#5711)

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

* AO3-6531 Fix comment decorator 500 error when parent comment is physically deleted

* AO3-6531 Add rake task to create placeholders for orphaned comments

* AO3-6531 Rename orphaned_commentable_ids variable to missing_commentable_ids.Suggested by brianjaustin.

AO3-7378 Updates for code review

AO3-6531 Fix create_placeholders_for_orphaned_comments task 500 (otwcode#5736)

* AO3-6531 - Add spec for orphaned comment with nil threaded_left

* AO3-6531 - Handle nil threaded_left in orphaned comment placeholder task

AO3-7381 Update Devise to 5.0.3 (otwcode#5727)

AO3-6531 Remove after task for placeholder comments (otwcode#5740)

Per comments on the related ticket, this could lead to weird behaviour.
Pulling this out so the fix for the most critical part can go live.

AO3-7080 Fix suspended and banned user check (otwcode#5741)

AO3-7378 add double trailing whitespace to try to get it to let me push whitespace changes after settings changes

AO3-7378 Add trailing new line

AO3-7378 Get my local back to just one line of trailing whitespace

AO3-7378 Still trying to get trailing whitespace settings to cooperate

AO3-7378 Trailing whitespace adjustment
jmalark added a commit to jmalark/otwarchive that referenced this pull request Apr 19, 2026
AO3-6229 Add static 404 error page (otwcode#5711)

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

* AO3-6531 Fix comment decorator 500 error when parent comment is physically deleted

* AO3-6531 Add rake task to create placeholders for orphaned comments

* AO3-6531 Rename orphaned_commentable_ids variable to missing_commentable_ids.Suggested by brianjaustin.

AO3-7378 Updates for code review

AO3-6531 Fix create_placeholders_for_orphaned_comments task 500 (otwcode#5736)

* AO3-6531 - Add spec for orphaned comment with nil threaded_left

* AO3-6531 - Handle nil threaded_left in orphaned comment placeholder task

AO3-7381 Update Devise to 5.0.3 (otwcode#5727)

AO3-6531 Remove after task for placeholder comments (otwcode#5740)

Per comments on the related ticket, this could lead to weird behaviour.
Pulling this out so the fix for the most critical part can go live.

AO3-7080 Fix suspended and banned user check (otwcode#5741)

AO3-7378 add double trailing whitespace to try to get it to let me push whitespace changes after settings changes

AO3-7378 Add trailing new line

AO3-7378 Get my local back to just one line of trailing whitespace

AO3-7378 Still trying to get trailing whitespace settings to cooperate

AO3-7378 Trailing whitespace adjustment

AO3-6229 Add static 404 error page (otwcode#5711)

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

* AO3-6531 Fix comment decorator 500 error when parent comment is physically deleted

* AO3-6531 Add rake task to create placeholders for orphaned comments

* AO3-6531 Rename orphaned_commentable_ids variable to missing_commentable_ids.Suggested by brianjaustin.

AO3-7378 Updates for code review

AO3-6531 Fix create_placeholders_for_orphaned_comments task 500 (otwcode#5736)

* AO3-6531 - Add spec for orphaned comment with nil threaded_left

* AO3-6531 - Handle nil threaded_left in orphaned comment placeholder task

AO3-7381 Update Devise to 5.0.3 (otwcode#5727)

AO3-6531 Remove after task for placeholder comments (otwcode#5740)

Per comments on the related ticket, this could lead to weird behaviour.
Pulling this out so the fix for the most critical part can go live.

AO3-7080 Fix suspended and banned user check (otwcode#5741)

AO3-7378 add double trailing whitespace to try to get it to let me push whitespace changes after settings changes

AO3-7378 Add trailing new line

AO3-7378 Get my local back to just one line of trailing whitespace

AO3-7378 Still trying to get trailing whitespace settings to cooperate

AO3-7378 Trailing whitespace adjustment

AO3-6229 Add static 404 error page (otwcode#5711)

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

* AO3-6531 Fix comment decorator 500 error when parent comment is physically deleted

* AO3-6531 Add rake task to create placeholders for orphaned comments

* AO3-6531 Rename orphaned_commentable_ids variable to missing_commentable_ids.Suggested by brianjaustin.

AO3-7378 Updates for code review

AO3-6531 Fix create_placeholders_for_orphaned_comments task 500 (otwcode#5736)

* AO3-6531 - Add spec for orphaned comment with nil threaded_left

* AO3-6531 - Handle nil threaded_left in orphaned comment placeholder task

AO3-7381 Update Devise to 5.0.3 (otwcode#5727)

AO3-6531 Remove after task for placeholder comments (otwcode#5740)

Per comments on the related ticket, this could lead to weird behaviour.
Pulling this out so the fix for the most critical part can go live.

AO3-7080 Fix suspended and banned user check (otwcode#5741)

AO3-7378 add double trailing whitespace to try to get it to let me push whitespace changes after settings changes

AO3-7378 Add trailing new line

AO3-7378 Get my local back to just one line of trailing whitespace

AO3-7378 Still trying to get trailing whitespace settings to cooperate

AO3-7378 Trailing whitespace adjustment

AO3-6229 Add static 404 error page (otwcode#5711)

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

* AO3-6531 Fix comment decorator 500 error when parent comment is physically deleted

* AO3-6531 Add rake task to create placeholders for orphaned comments

* AO3-6531 Rename orphaned_commentable_ids variable to missing_commentable_ids.Suggested by brianjaustin.

AO3-7378 Updates for code review

AO3-6531 Fix create_placeholders_for_orphaned_comments task 500 (otwcode#5736)

* AO3-6531 - Add spec for orphaned comment with nil threaded_left

* AO3-6531 - Handle nil threaded_left in orphaned comment placeholder task

AO3-7381 Update Devise to 5.0.3 (otwcode#5727)

AO3-6531 Remove after task for placeholder comments (otwcode#5740)

Per comments on the related ticket, this could lead to weird behaviour.
Pulling this out so the fix for the most critical part can go live.

AO3-7080 Fix suspended and banned user check (otwcode#5741)

AO3-7378 add double trailing whitespace to try to get it to let me push whitespace changes after settings changes

AO3-7378 Add trailing new line

AO3-7378 Get my local back to just one line of trailing whitespace

AO3-7378 Still trying to get trailing whitespace settings to cooperate

AO3-7378 Trailing whitespace adjustment

Revert

AO3-6229 Add static 404 error page (otwcode#5711)

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

* AO3-6531 Fix comment decorator 500 error when parent comment is physically deleted

* AO3-6531 Add rake task to create placeholders for orphaned comments

* AO3-6531 Rename orphaned_commentable_ids variable to missing_commentable_ids.Suggested by brianjaustin.

AO3-6531 Fix create_placeholders_for_orphaned_comments task 500 (otwcode#5736)

* AO3-6531 - Add spec for orphaned comment with nil threaded_left

* AO3-6531 - Handle nil threaded_left in orphaned comment placeholder task

AO3-7381 Update Devise to 5.0.3 (otwcode#5727)

AO3-6531 Remove after task for placeholder comments (otwcode#5740)

Per comments on the related ticket, this could lead to weird behaviour.
Pulling this out so the fix for the most critical part can go live.

AO3-7080 Fix suspended and banned user check (otwcode#5741)

Try to revert unintended change

Revert

Add trailing whitespace

Try to name and use variable on same line
jmalark added a commit to jmalark/otwarchive that referenced this pull request Apr 20, 2026
AO3-6229 Add static 404 error page (otwcode#5711)

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

* AO3-6531 Fix comment decorator 500 error when parent comment is physically deleted

* AO3-6531 Add rake task to create placeholders for orphaned comments

* AO3-6531 Rename orphaned_commentable_ids variable to missing_commentable_ids.Suggested by brianjaustin.

AO3-6531 Fix create_placeholders_for_orphaned_comments task 500 (otwcode#5736)

* AO3-6531 - Add spec for orphaned comment with nil threaded_left

* AO3-6531 - Handle nil threaded_left in orphaned comment placeholder task

AO3-7381 Update Devise to 5.0.3 (otwcode#5727)

AO3-6531 Remove after task for placeholder comments (otwcode#5740)

Per comments on the related ticket, this could lead to weird behaviour.
Pulling this out so the fix for the most critical part can go live.

AO3-7080 Fix suspended and banned user check (otwcode#5741)

AO3-7378 Fix the order of en.yml
jmalark added a commit to jmalark/otwarchive that referenced this pull request Apr 22, 2026
AO3-6229 Add static 404 error page (otwcode#5711)

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

* AO3-6531 Fix comment decorator 500 error when parent comment is physically deleted

* AO3-6531 Add rake task to create placeholders for orphaned comments

* AO3-6531 Rename orphaned_commentable_ids variable to missing_commentable_ids.Suggested by brianjaustin.

AO3-6531 Fix create_placeholders_for_orphaned_comments task 500 (otwcode#5736)

* AO3-6531 - Add spec for orphaned comment with nil threaded_left

* AO3-6531 - Handle nil threaded_left in orphaned comment placeholder task

AO3-7381 Update Devise to 5.0.3 (otwcode#5727)

AO3-6531 Remove after task for placeholder comments (otwcode#5740)

Per comments on the related ticket, this could lead to weird behaviour.
Pulling this out so the fix for the most critical part can go live.

AO3-7080 Fix suspended and banned user check (otwcode#5741)

AO3-7378 Fix the order of en.yml
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.

2 participants