From fcfdeadc04983ab6937df6ce6a04dbe38e33b1a5 Mon Sep 17 00:00:00 2001 From: Claire Date: Fri, 29 Dec 2023 17:23:07 +0100 Subject: [PATCH] Fix random `NoMethodError` errors on cached `CustomFilter` objects (#28521) --- app/models/custom_filter.rb | 19 +++++++++++++++++-- app/models/custom_filter_keyword.rb | 10 +++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/models/custom_filter.rb b/app/models/custom_filter.rb index 5a53e73ba..9c936fed3 100644 --- a/app/models/custom_filter.rb +++ b/app/models/custom_filter.rb @@ -17,8 +17,23 @@ class CustomFilter < ApplicationRecord self.ignored_columns += %w(whole_word irreversible) - alias_attribute :title, :phrase - alias_attribute :filter_action, :action + # NOTE: We previously used `alias_attribute` but this does not play nicely + # with cache + def title + phrase + end + + def title=(value) + self.phrase = value + end + + def filter_action + action + end + + def filter_action=(value) + self.action = value + end VALID_CONTEXTS = %w( home diff --git a/app/models/custom_filter_keyword.rb b/app/models/custom_filter_keyword.rb index 3158b3b79..1812a4308 100644 --- a/app/models/custom_filter_keyword.rb +++ b/app/models/custom_filter_keyword.rb @@ -17,7 +17,15 @@ class CustomFilterKeyword < ApplicationRecord validates :keyword, presence: true - alias_attribute :phrase, :keyword + # NOTE: We previously used `alias_attribute` but this does not play nicely + # with cache + def phrase + keyword + end + + def phrase=(value) + self.keyword = value + end before_save :prepare_cache_invalidation! before_destroy :prepare_cache_invalidation!