ConditionalCounterCache
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.
Give condition when to increment/decrement counter cache.
Usage
Customize condition via :counter_cache option:
class Tagging < ActiveRecord::Base
belongs_to :item
belongs_to :tag, counter_cache: { condition: -> { !item.private? } }
endOther examples:
belongs_to :tag, counter_cache: true
belongs_to :tag, counter_cache: "items_count"
belongs_to :tag, counter_cache: { condition: -> { !item.private? } }
belongs_to :tag, counter_cache: { condition: -> :your_favorite_method_name }
belongs_to :tag, counter_cache: { column_name: "items_count" }
belongs_to :tag, counter_cache: { column_name: "items_count", condition: -> { !item.private? } }