Fix: ActiveRecord::RecordInvalid (MAYBE-RAILS-D5) #1736
+53
−222
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 "ActiveRecord::RecordInvalid: Validation failed: Outflow transaction has already been taken" occurs because the account syncing process is attempting to create duplicate transfers where a transaction is being reused in multiple transfers. This violates the uniqueness validations defined in the Transfer model.
The root cause is in the
transfer_match_candidates
method in account.rb. The original LEFT JOIN clause only checked if either the inflow_transaction_id OR outflow_transaction_id matched exactly, but didn't properly exclude cases where a transaction ID might be used in the opposite role in an existing transfer.Key changes:
Modified the LEFT JOIN clause to check both transaction IDs against both the inflow and outflow fields of existing transfers. This ensures that if a transaction is used in ANY existing transfer (whether as inflow or outflow), it won't be considered as a candidate for new transfers.
The new JOIN clause uses IN clauses to check both candidate transaction IDs against both fields of existing transfers, providing complete coverage of all possible combinations.
Added comprehensive test coverage to verify the fix, including edge cases where:
This fix maintains the original intent of the auto-matching feature while preventing the uniqueness validation error by properly excluding any transaction that's already part of a transfer.
The test suite additions validate both the happy path (successful matches) and edge cases (preventing duplicate transfers), ensuring the fix is robust and maintainable.
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