r7kamura / conditional_counter_cache

Give condition when to increment/decrement counter cache.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Give condition when to increment/decrement counter cache.

License:MIT License


Languages

Language:Ruby 100.0%