Fix: ActiveRecord::RecordInvalid (MAYBE-RAILS-DM) #1801
Closed
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 because the system is attempting to create duplicate Transfer records where transactions are already associated with existing transfers. The root cause is in the
transfer_match_candidates
method in Account model, which wasn't properly excluding transactions that are already part of transfers.The original code's intent was to match transactions between accounts that could represent transfers (same amount but opposite signs, within 4 days of each other, different accounts). However, the LEFT JOIN conditions weren't comprehensive enough to exclude all cases where transactions were already associated with transfers.
Key changes made:
Modified the
existing_transfers
JOIN condition to check both transactions against both inflow and outflow fields. This ensures we catch cases where either transaction is already part of any transfer, regardless of its role.Enhanced the
rejected_transfers
JOIN to check for rejected transfers in both directions (A->B and B->A). This prevents re-matching previously rejected transfer pairs.Added a new test
auto_match_transfers_respects_existing_transfers
to verify that transactions already in transfers aren't re-matched.The updated query is more thorough in excluding existing transfers while maintaining the original matching logic for legitimate transfer candidates.
Error Details
Summary:
Stacktrace:
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?
Important
If something doesn’t look right, click to retry this interaction.
Quick links: View in Sentry • View in Revise