From dd1a7fa8cb6c9b69e8fc6b170f5b83d955454435 Mon Sep 17 00:00:00 2001 From: Josh Pigford Date: Mon, 3 Feb 2025 11:18:27 -0600 Subject: [PATCH] Improve holding records generation with security preloading --- app/models/account/holding_calculator.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/models/account/holding_calculator.rb b/app/models/account/holding_calculator.rb index 5f6673de..205a422f 100644 --- a/app/models/account/holding_calculator.rb +++ b/app/models/account/holding_calculator.rb @@ -48,8 +48,9 @@ class Account::HoldingCalculator def generate_holding_records(portfolio, date) portfolio.map do |security_id, qty| security = securities_cache[security_id] - price = security.dig(:prices)&.find { |p| p.date == date } + next if security.nil? + price = security.dig(:prices)&.find { |p| p.date == date } next if price.blank? converted_price = Money.new(price.price, price.currency).exchange_to(account.currency, fallback_rate: 1).amount @@ -106,7 +107,10 @@ class Account::HoldingCalculator end def preload_securities + # Get securities from trades and current holdings securities = trades.map(&:entryable).map(&:security).uniq + securities += account.holdings.where(date: Date.current).map(&:security) + securities.uniq! securities.each do |security| prices = Security::Price.find_prices(