The personal finance app for everyone (by everyone)
Ruby
Address PR review feedback
eureka928029d09685ee5
diff --git a/app/jobs/clear_ai_cache_job.rb b/app/jobs/clear_ai_cache_job.rbindex bff65fa793..0592938189 100644--- a/app/jobs/clear_ai_cache_job.rb+++ b/app/jobs/clear_ai_cache_job.rb@@ -2,14 +2,27 @@ class ClearAiCacheJob < ApplicationJobqueue_as :low_prioritydef perform(family)+ if family.nil?+ Rails.logger.warn("ClearAiCacheJob called with nil family, skipping")+ return+ end+Rails.logger.info("Clearing AI cache for family #{family.id}")# Clear AI enrichment data for transactions- Transaction.clear_ai_cache(family)- Rails.logger.info("Cleared AI cache for transactions")+ begin+ Transaction.clear_ai_cache(family)+ Rails.logger.info("Cleared AI cache for transactions")+ rescue => e+ Rails.logger.error("Failed to clear AI cache for transactions: #{e.message}")+ end# Clear AI enrichment data for entries- Entry.clear_ai_cache(family)- Rails.logger.info("Cleared AI cache for entries")+ begin+ Entry.clear_ai_cache(family)+ Rails.logger.info("Cleared AI cache for entries")+ rescue => e+ Rails.logger.error("Failed to clear AI cache for entries: #{e.message}")+ endendenddiff --git a/app/models/concerns/enrichable.rb b/app/models/concerns/enrichable.rbindex 04c557dc65..d963c663d2 100644--- a/app/models/concerns/enrichable.rb+++ b/app/models/concerns/enrichable.rb@@ -150,17 +150,23 @@ module Enrichabledef clear_ai_cacheActiveRecord::Base.transaction do- # Find attributes that were locked by AI enrichment- ai_enriched_attrs = data_enrichments.where(source: "ai").pluck(:attribute_name).uniq-- # Batch unlock all AI-enriched attributes in a single update- if ai_enriched_attrs.any?- new_locked_attrs = locked_attributes.except(*ai_enriched_attrs)+ ai_enrichments = data_enrichments.where(source: "ai")++ # Only unlock attributes where current value still matches what AI set+ # If user changed the value, they took ownership - don't unlock+ attrs_to_unlock = ai_enrichments.select do |enrichment|+ current_value = send(enrichment.attribute_name) rescue self[enrichment.attribute_name]+ current_value.to_s == enrichment.value.to_s+ end.map(&:attribute_name).uniq++ # Batch unlock in a single update+ if attrs_to_unlock.any?+ new_locked_attrs = locked_attributes.except(*attrs_to_unlock)update_column(:locked_attributes, new_locked_attrs) if new_locked_attrs != locked_attributesend# Delete AI enrichment records- data_enrichments.where(source: "ai").delete_all+ ai_enrichments.delete_allendend