Skip to content
kennuzzo edited this page Apr 10, 2015 · 2 revisions

Referencing the stack overflow question on how to cache the tags.

the example model

class Product < ActiveRecord::Base
  acts_as_taggable_on :categories
end

the caching migration:

class AddCachedCategoryListToProducts < ActiveRecord::Migration
  def self.up
    add_column :products,  :cached_category_list, :string
    Product.reset_column_information
    # next line makes ActsAsTaggableOn see the new column and create cache methods
    ActsAsTaggableOn::Taggable::Cache.included(Product)
    Product.find_each(:batch_size => 1000) do |p|
      p.category_list # it seems you need to do this first to generate the list
      p.save!
    end    
  end
end
Clone this wiki locally