Fix: TypeError (MAYBE-RAILS-DH) #1767
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 some holdings in the system have nil amounts, but the code attempts to sum these amounts directly using
sum(&:amount)
. This fails because Ruby cannot coerce nil into a BigDecimal for addition.The original intent of the code was to calculate the total value of holdings for a specific date by summing their amounts. However, it didn't account for the possibility of nil amounts, which can occur in real-world scenarios (e.g., when price data is missing or during data import processes).
The fix involves two main changes:
In BalanceTrendCalculator, we modify the holdings summation to handle nil values by:
map(&:amount)
compact
to remove any nil valuesWe add a comprehensive test suite to verify the behavior:
This approach maintains the original functionality while making it more robust against nil values, which is particularly important in financial calculations where data integrity is crucial.
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