Flowdock
method

increment_counter

Importance_3
Ruby on Rails latest stable (v3.2.13) - 2 notes - Class: ActiveRecord::Base

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v2.3.8) is shown here.

These similar methods exist in v3.2.13:

increment_counter(counter_name, id) public

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)
Show source
Register or log in to add new notes.
September 30, 2009
2 thanks

See also: ActiveRecord::Base#increment

This is a class-level method. For the instance-level equivalent see: ActiveRecord::Base#increment

item = Item.find(1)
item.foo_count # => 0
Item.increment_counter(:foo_count, 1)
item.foo_count # => 0
item.reload
item.foo_count # => 1
item.increment(:foo_count)
item.foo_count # => 2
March 5, 2012
0 thanks

won't refresh updated_at

This will not cause :updated_at column to refresh, while ActiveRecord::Base#increment! would.