instance method increment_counter

Ruby on Rails 3.0.20

Since v3.0.20 Last seen in v3.2.22.5

Available in: v3.0.20 v3.1.12 v3.2.22.5

Signature

increment_counter(counter_name, id)

Increment a number field by one, usually representing a count.

This is used for caching aggregate values, so that they don’t need to be computed every time. For example, a DiscussionBoard may cache post_count and comment_count otherwise every time the board is shown it would have to run an SQL query to find how many posts and comments there are.

Parameters

  • counter_name - The name of the field that should be incremented.

  • id - The id of the object that should be incremented.

Examples

# Increment the post_count column for the record with an id of 5
DiscussionBoard.increment_counter(:post_count, 5)

Parameters

counter_name req
id req
Source
# File activerecord/lib/active_record/counter_cache.rb, line 94
    def increment_counter(counter_name, id)
      update_counters(id, counter_name => 1)
    end

Defined in activerecord/lib/active_record/counter_cache.rb line 94 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::CounterCache

Type at least 2 characters to search.

↑↓ navigate · open · esc close