Module: BetterCounters::ClassMethods

Defined in:
lib/better_counters.rb

Instance Method Summary collapse

Instance Method Details

#counter_cache(p) ⇒ Object

*Enable counter caching*

Takes a Hash where keys are the association singular name, and the values are either true or a custom counter column name.

Example:

>> counter_cache :user => true

You can override the counter column name by replacing true with the name.

>> counter_cache :user => :total_tasks

Arguments p: (Hash)



29
30
31
32
# File 'lib/better_counters.rb', line 29

def counter_cache(p)
  after_create  Proc.new { update_counter_cache(p, 1) }
  after_destroy Proc.new { update_counter_cache(p, -1) }
end