VolatileCounterCache
Note
This project is archived because I no longer actively use it, and there seems to be no recent activity from other users either. If you are still using it and would like to take over maintenance, please contact me somewhere.
Provides volatile counter cache logic to ActiveRecord::Base.
Usage
-
favorites_counttries to read counter cache from Rails.cache. - If there is no cache yet, it calls
favorites.sizeand store the result. - When
favoriteis created or destroyed, the cache is purged.
class Tweet < ActiveRecord::Base
has_many :favorites
volatile_counter_cache :favorites, cache: Rails.cache
end
Tweet.first.favorites_count #=> 42
Tweet.first.favorites.first.destroy
Tweet.first.favorites_count #=> 41options
- cache [ActiveSupport::Cache::Store] A store object to store count for each key.
- cache_options [Hash] Options for cache store.
- counter_method_name [String] An optional String to change counter method name.
- foreign_key [Symbol] An optional Symbol to change cache key at callback.