Skip to content

Conversation

@rajnish-jarvis
Copy link

When using soft-delete strategies (e.g., Paranoia or Discard), soft-deleted records may retain their original position value. This causes a conflict during the after_destroy callback in acts_as_list, particularly in the decrement_positions_on_lower_items method.

Currently, the callback uses:
acts_as_list_list.where("position > ?", deleted_position)

This assumes all records are visible, but if a soft-deleted record still holds the deleted position, the next item is moved into a position that's already taken — causing duplicates.

What this commit does:

  • Modifies acts_as_list_list to include soft-deleted records when with_deleted is available.
  • Adjusts decrement_positions_on_lower_items to skip updating any record if the target position is already occupied (even by a soft-deleted record).
  • Ensures uniqueness of position values is preserved even in the presence of soft-deleted items.

This fix maintains backward compatibility and improves behavior in applications using soft-delete mechanisms with acts_as_list.

handling with deleted
@brendon
Copy link
Owner

brendon commented Jun 17, 2025

Hi @rajnish-jarvis, thanks for the PR.

Unfortunately I'm not super keen to be sniffing for arbitrary soft delete libraries and adjusting course like that. I think you could also solve this problem by introducing the soft delete column into the scope of the list. That way when an item is soft deleted it will leave the visible list and be assigned an order based on its new scope. Undeleting the item will move it to the end of the list but that's probably desired anyway since something would have taken its place already.

Give that a go and let me know how you go. I'll close this for now but feel free to communicate with me here :)

@brendon brendon closed this Jun 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants