diff --git a/lib/acts_as_taggable_on/taggable/core.rb b/lib/acts_as_taggable_on/taggable/core.rb index 22218f994..6f02663bf 100644 --- a/lib/acts_as_taggable_on/taggable/core.rb +++ b/lib/acts_as_taggable_on/taggable/core.rb @@ -275,7 +275,7 @@ def tag_list_cache_on(context) variable_name = "@#{context.to_s.singularize}_list" if instance_variable_get(variable_name) instance_variable_get(variable_name) - elsif cached_tag_list_on(context) && self.class.caching_tag_list_on?(context) + elsif cached_tag_list_on(context) && ensure_included_cache_methods! && self.class.caching_tag_list_on?(context) instance_variable_set(variable_name, ActsAsTaggableOn.default_parser.new(cached_tag_list_on(context)).parse) else instance_variable_set(variable_name, ActsAsTaggableOn::TagList.new(tags_on(context).map(&:name))) @@ -418,6 +418,10 @@ def save_tags private + def ensure_included_cache_methods! + self.class.columns + end + # Filters the tag lists from the attribute names. def attributes_for_update(attribute_names) tag_lists = tag_types.map {|tags_type| "#{tags_type.to_s.singularize}_list"} diff --git a/spec/acts_as_taggable_on/caching_spec.rb b/spec/acts_as_taggable_on/caching_spec.rb index 18acae984..a9f762c4a 100644 --- a/spec/acts_as_taggable_on/caching_spec.rb +++ b/spec/acts_as_taggable_on/caching_spec.rb @@ -99,6 +99,24 @@ end end + describe 'Cache methods initialization on new models' do + before(:all) do + ActiveRecord::Base.connection.execute( + 'INSERT INTO cache_methods_injected_models (cached_tag_list) VALUES (\'ciao\')' + ) + class CacheMethodsInjectedModel < ActiveRecord::Base + acts_as_taggable + end + end + after(:all) { Object.send(:remove_const, :CacheMethodsInjectedModel) } + + it 'cached_tag_list_on? get injected correctly' do + expect do + CacheMethodsInjectedModel.first.tag_list + end.not_to raise_error + end + end + describe 'CachingWithArray' do pending '#TODO' end diff --git a/spec/internal/db/schema.rb b/spec/internal/db/schema.rb index d72f881cb..af9ba2ab4 100644 --- a/spec/internal/db/schema.rb +++ b/spec/internal/db/schema.rb @@ -78,6 +78,9 @@ t.column :type, :string end + create_table :cache_methods_injected_models, force: true do |t| + t.column :cached_tag_list, :string + end # Special cases for postgresql if using_postgresql?