Fix: ActiveRecord::DeleteRestrictionError (MAYBE-RAILS-CA) #1613
+18
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The error occurs when attempting to delete an Account::Transaction record that is part of a transfer relationship. The current implementation uses
dependent: :restrict_with_exception
on both the transfer_as_inflow and transfer_as_outflow associations, which prevents deletion of transactions that are part of transfers. This was likely implemented to prevent orphaned transfer records.However, this restriction is overly protective. When deleting a transaction that's part of a transfer, it makes more sense to automatically delete the associated transfer record rather than preventing the deletion entirely. This maintains data integrity while providing a better user experience.
Changes made:
dependent: :destroy
instead ofdependent: :restrict_with_exception
This change aligns with Rails conventions for handling dependent records and provides a more flexible solution while maintaining data integrity. The original intent of preventing orphaned transfers is still maintained, but now it's handled through cascading deletes rather than restrictions.
Tip
You can make revisions or ask questions of Revise.dev by using
/revise
in any comment or review!/revise Add a comment above the method to explain why we're making this change.
/revise Why did you choose to make this change specifically?