/conditional_counter_cache

Give condition when to increment/decrement counter cache.

Primary LanguageRubyMIT LicenseMIT

ConditionalCounterCache

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? } }
end

Other 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? } }

See also