Skip to main content

caching_tag_counts

📈 Caching Tag Counts for Performance​

For large data sets, counting tags on the fly can be slow. Enable caching to store tag counts on your models, speeding up queries and views.

# Generate a migration to add cached column
rails generate migration AddCachedTagListToPosts cached_tag_list:string

# In your model
class Post < ApplicationRecord
acts_as_taggable_on :tags
acts_as_taggable_on :tags, cache_column: :cached_tag_list
end

Rebuild the cache for existing records:

Post.find_each(&:save)

Now you can search by cached tags directly and avoid heavy joins:

Post.where("cached_tag_list LIKE ?", "%ruby%")