Fix demo loan schedules inside generation transactions

Merged
JJ
2mo ago
+59-1

Summary

Reload each newly created demo loan before reading its amortization schedule.

The has_one :account, touch: true callback can cache a missing Account while the Loan is saved before its parent. Rails clears that negative association cache after commit, but sample-data generation and demo refresh generate loan payments inside the surrounding transaction. The cached nil makes amortizable? return false, then payment generation raises NoMethodError on nil.payments.

The change is scoped to the loan-payment loop. Normal currencies, FinanceKit generation, configured FX/market-data providers and sync scheduling keep their existing behavior.

Regression coverage

  • Existing-family generate_new_user_data_for!
  • Default demo generation inside an outer refresh transaction

Both tests create the real loan accounts, opening valuations and scheduled payments. They check payment counts and principal totals for all three loans. Both failed with the original nil-schedule error before the fix.

Validation

  • Full Rails suite: 10,453 runs, 44,091 assertions, 0 failures, 0 errors, 33 skips
  • Demo generator tests: 10 runs, 67 assertions, 0 failures, 0 errors
  • Loan payoff chart system tests: 6 runs, 47 assertions, 0 failures, 0 errors
  • Full local system suite: 186 runs, 943 assertions, 0 failures, 0 errors, 0 skips
  • Full Ruby lint passed
  • ERB lint passed across 745 templates
  • Biome passed
  • Brakeman: 0 errors, 0 security warnings
  • git diff --check passed

Validated with Ruby 3.4.9 and Rails 8.1.3.1 against main at ec4282e0e.

Summary by CodeRabbit

  • Bug Fixes
    • Loan payment generation now uses the latest loan schedule, helping ensure payments due by the current date are recorded accurately for mortgages, student loans, and car loans. This keeps generated payment counts and principal totals aligned with each loan’s scheduled repayment plan.