Method: Mongoid::Association::Referenced::CounterCache::ClassMethods#increment_counter

Defined in:
lib/mongoid/association/referenced/counter_cache.rb

#increment_counter(counter_name, id) ⇒ Object

Increment the counter name from the entries that match the id by one. This method is used on associations callbacks when counter_cache is enabled

Examples:

Increment comments counter

Post.increment_counter(:comments_count, '50e0edd97c71c17ea9000001')

Parameters:

  • counter_name (Symbol)

    Counter cache name

  • id (String)

    The id of the object that will have its counter incremented.



71
72
73
# File 'lib/mongoid/association/referenced/counter_cache.rb', line 71

def increment_counter(counter_name, id)
  update_counters(id, counter_name.to_sym => 1)
end