From f72b0a81e9d48b0998d5337eda0443c2e4752746 Mon Sep 17 00:00:00 2001 From: Mohamed El-Sawy Date: Mon, 4 Nov 2024 18:48:25 +0200 Subject: [PATCH] CV2-5601: apply the background job if the obj exists (#2119) --- app/lib/check_cached_fields.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/lib/check_cached_fields.rb b/app/lib/check_cached_fields.rb index 5af81b8d3..fedc37191 100644 --- a/app/lib/check_cached_fields.rb +++ b/app/lib/check_cached_fields.rb @@ -143,12 +143,14 @@ def update_cached_field(name, obj, condition, ids, callback, options, event) def update_cached_field_bg(name, ids, callback, options, klass, id, event) obj = event == 'destroy' ? klass.constantize : klass.constantize.find_by_id(id) - recalculate = options[:recalculate] - self.where(id: ids).each do |target| - value = callback == :recalculate ? target.send(recalculate) : obj.send(callback, target) - Rails.cache.write(self.check_cache_key(self, target.id, name), value, expires_in: self.cached_field_expiration(options)) - # Update ES index and PG, if needed - self.index_and_pg_cached_field(options, value, name, target) + unless obj.nil? + recalculate = options[:recalculate] + self.where(id: ids).each do |target| + value = callback == :recalculate ? target.send(recalculate) : obj.send(callback, target) + Rails.cache.write(self.check_cache_key(self, target.id, name), value, expires_in: self.cached_field_expiration(options)) + # Update ES index and PG, if needed + self.index_and_pg_cached_field(options, value, name, target) + end end end end