8000 Fix: TypeError (MAYBE-RAILS-DH) by revise-dev[bot] · Pull Request #1767 · maybe-finance/maybe · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix: TypeError (MAYBE-RAILS-DH) #1767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

revise-dev[bot]
Copy link
@revise-dev revise-dev bot commented Feb 1, 2025

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:

  1. In BalanceTrendCalculator, we modify the holdings summation to handle nil values by:

    • First mapping the amounts using map(&:amount)
    • Then using compact to remove any nil values
    • Finally summing the remaining valid amounts
  2. We add a comprehensive test suite to verify the behavior:

    • Tests the handling of nil amounts in holdings
    • Verifies that the calculator continues to work correctly with mixed nil and non-nil amounts
    • Ensures the trend calculation remains accurate when some holdings have nil amounts
    • Follows the existing test patterns seen in the test suite

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:

TypeError: nil can't be coerced into BigDecimal (TypeError)

    todays_holdings_value = holdings.select { |h| h.date == entry.date }.sum(&:amount)
                                                                             ^^^^^^^^

Stacktrace:

app/models/account/balance_trend_calculator.rb:41
app/models/account/balance_trend_calculator.rb:41
app/models/account/balance_trend_calculator.rb:41
app/views/accounts/show/_activity.html.erb:81
app/helpers/account/entries_helper.rb:14
action_view/helpers/capture_helper.rb:50
action_view/buffers.rb:75
action_view/helpers/capture_helper.rb:50
app/helpers/account/entries_helper.rb:13
app/helpers/account/entries_helper.rb:12
app/helpers/account/entries_helper.rb:12
app/helpers/account/entries_helper.rb:12
action_view/helpers/capture_helper.rb:50
action_view/buffers.rb:75
action_view/helpers/capture_helper.rb:50
action_view/helpers/tag_helper.rb:243
action_view/helpers/tag_helper.rb:463
action_view/helpers/capture_helper.rb:50
action_view/buffers.rb:75
action_view/helpers/capture_helper.rb:50
action_view/helpers/tag_helper.rb:243
action_view/helpers/tag_helper.rb:342
/home/deploy/maybe-production/shared/bundle/ruby/3.4.0/gems/turbo-rails-2.0.11/app/helpers/turbo/frames_helper.rb:42
action_view/base.rb:268
action_view/base.rb:268
action_view/template.rb:285
active_support/notifications.rb:210
active_support/notifications/instrumenter.rb:58
sentry/rails/tracing.rb:56
active_support/notifications.rb:210
action_view/template.rb:584
action_view/template.rb:273
action_view/renderer/partial_renderer.rb:252
active_support/notifications.rb:210
active_support/notifications/instrumenter.rb:58
sentry/rails/tracing.rb:56
active_support/notifications.rb:210
action_view/renderer/partial_renderer.rb:246
action_view/renderer/partial_renderer.rb:237
action_view/renderer/renderer.rb:78
action_view/renderer/renderer.rb:49
action_view/helpers/rendering_helper.rb:44
action_view/helpers/capture_helper.rb:50
action_view/buffers.rb:75
action_view/helpers/capture_helper.rb:50
action_view/helpers/tag_helper.rb:243
action_view/helpers/tag_helper.rb:463
action_view/helpers/capture_helper.rb:50
action_view/buffers.rb:75
action_view/helpers/capture_helper.rb:50
action_view/helpers/tag_helper.rb:243
action_view/helpers/tag_helper.rb:342
/home/deploy/maybe-production/shared/bundle/ruby/3.4.0/gems/turbo-rails-2.0.11/app/helpers/turbo/frames_helper.rb:42
action_view/base.rb:281
action_view/base.rb:281
action_view/template.rb:285
active_support/notifications.rb:210
active_support/notifications/instrumenter.rb:58
sentry/rails/tracing.rb:56
active_support/notifications.rb:210
action_view/template.rb:584
action_view/template.rb:273
action_view/renderer/template_renderer.rb:66
active_support/notifications.rb:210
active_support/notifications/instrumenter.rb:58
sentry/rails/tracing.rb:56
active_support/notifications.rb:210
action_view/renderer/template_renderer.rb:60
action_view/renderer/template_renderer.rb:76
active_support/notifications.rb:210
active_support/notifications/instrumenter.rb:58
sentry/rails/tracing.rb:56
active_support/notifications.rb:210
action_view/renderer/template_renderer.rb:75
action_view/renderer/template_renderer.rb:59
action_view/renderer/template_renderer.rb:11
action_view/renderer/renderer.rb:58
action_view/renderer/renderer.rb:31
action_view/rendering.rb:135
action_view/base.rb:308
action_view/rendering.rb:134
action_controller/metal/streaming.rb:258
action_view/rendering.rb:121
action_controller/metal/rendering.rb:186
action_controller/metal/renderers.rb:142
abstract_controller/rendering.rb:29
action_controller/metal/rendering.rb:167
action_controller/metal/instrumentation.rb:32
benchmark.rb:323
active_support/core_ext/benchmark.rb:14
action_controller/metal/instrumentation.rb:32
action_controller/metal/instrumentation.rb:101
active_record/railties/controller_runtime.rb:46
action_controller/metal/instrumentation.rb:31
action_controller/metal/implicit_render.rb:39
action_controller/metal/basic_implicit_render.rb:9
abstract_controller/base.rb:226
action_controller/metal/rendering.rb:193
abstract_controller/callbacks.rb:261
active_support/callbacks.rb:121
active_support/core_ext/time/zones.rb:65
app/controllers/concerns/localize.rb:17
active_support/callbacks.rb:130
i18n.rb:353
app/controllers/concerns/localize.rb:12
active_support/callbacks.rb:130
turbo-rails.rb:24
/home/deploy/maybe-production/shared/bundle/ruby/3.4.0/gems/turbo-rails-2.0.11/app/controllers/concerns/turbo/request_id_tracking.rb:10
active_support/callbacks.rb:130
action_text/rendering.rb:25
action_text/engine.rb:71
active_support/callbacks.rb:130
active_support/callbacks.rb:130
sentry/rails/controller_transaction.rb:21
sentry/hub.rb:115
sentry/span.rb:237
sentry/hub.rb:113
sentry-ruby.rb:506
sentry/rails/controller_transaction.rb:18
active_support/callbacks.rb:130
active_support/callbacks.rb:141
abstract_controller/callbacks.rb:260
action_controller/metal/rescue.rb:27
action_controller/metal/instrumentation.rb:77
active_support/notifications.rb:210
active_support/notifications/instrumenter.rb:58
sentry/rails/tracing.rb:56
active_support/notifications.rb:210
action_controller/metal/instrumentation.rb:76
action_controller/metal/params_wrapper.rb:259
active_record/railties/controller_runtime.rb:39
abstract_controller/base.rb:163
action_view/rendering.rb:40
action_controller/metal.rb:252
action_controller/metal.rb:335
action_dispatch/routing/route_set.rb:67
action_dispatch/routing/route_set.rb:50
action_dispatch/journey/router.rb:53
action_dispatch/journey/router.rb:133
action_dispatch/journey/router.rb:126
action_dispatch/journey/router.rb:126
action_dispatch/journey/router.rb:34
action_dispatch/routing/route_set.rb:896
rails-settings/middleware.rb:9
rack/tempfile_reaper.rb:20
rack/etag.rb:29
rack/conditional_get.rb:31
rack/head.rb:15
action_dispatch/http/permissions_policy.rb:38
action_dispatch/http/content_security_policy.rb:38
rack/session/abstract/id.rb:274
rack/session/abstract/id.rb:268
action_dispatch/middleware/cookies.rb:704
action_dispatch/middleware/callbacks.rb:31
active_support/callbacks.rb:101
action_dispatch/middleware/callbacks.rb:30
sentry/rails/rescued_exception_interceptor.rb:14
action_dispatch/middleware/debug_exceptions.rb:31
sentry/rack/capture_exceptions.rb:30
sentry/hub.rb:269
sentry-ruby.rb:419
sentry/rack/capture_exceptions.rb:21
sentry/hub.rb:59
sentry-ruby.rb:399
sentry/rack/capture_exceptions.rb:20
action_dispatch/middleware/show_exceptions.rb:32
rails/rack/logger.rb:41
rails/rack/logger.rb:29
action_dispatch/middleware/remote_ip.rb:96
action_dispatch/middleware/request_id.rb:33
rack/method_override.rb:28
rack/runtime.rb:24
action_dispatch/middleware/executor.rb:16
action_dispatch/middleware/static.rb:27
rack/sendfile.rb:114
action_dispatch/middleware/ssl.rb:82
action_dispatch/middleware/assume_ssl.rb:24
mini_profiler.rb:191
rails/engine.rb:535
puma/configuration.rb:279
puma/request.rb:99
puma/thread_pool.rb:389
puma/request.rb:98
puma/server.rb:468
puma/server.rb:249
puma/thread_pool.rb:166

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 SentryView in Revise

@zachgoll
Copy link
Collaborator
zachgoll commented Feb 3, 2025

This is the symptom, not the cause. See #1781

@zachgoll zachgoll closed this Feb 3, 2025
@Shpigford Shpigford deleted the revise/fix-maybe-rails-dh-1738451755 branch February 5, 2025 17:53
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.

1 participant
0