Flowdock
method

update_counters

Importance_2
Ruby on Rails latest stable (v6.1.7.7) - 0 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 v6.1.7.7:

update_counters(id, counters) public

A generic "counter updater" implementation, intended primarily to be used by increment_counter and decrement_counter, but which may also be useful on its own. It simply does a direct SQL update for the record with the given ID, altering the given hash of counters by the amount given by the corresponding value:

Parameters

  • id - The id of the object you wish to update a counter on or an Array of ids.
  • counters - An Array of Hashes containing the names of the fields to update as keys and the amount to update the field by as values.

Examples

  # For the Post with id of 5, decrement the comment_count by 1, and
  # increment the action_count by 1
  Post.update_counters 5, :comment_count => -1, :action_count => 1
  # Executes the following SQL:
  # UPDATE posts
  #    SET comment_count = comment_count - 1,
  #        action_count = action_count + 1
  #  WHERE id = 5

  # For the Posts with id of 10 and 15, increment the comment_count by 1
  Post.update_counters [10, 15], :comment_count => 1
  # Executes the following SQL:
  # UPDATE posts
  #    SET comment_count = comment_count + 1,
  #  WHERE id IN (10, 15)
Show source
Register or log in to add new notes.