class method self.update_counters

Ruby on Rails 2.3.18

Since v2.2.3 Last seen in v2.3.18

Available in: v2.2.3 v2.3.18

Signature

self.update_counters(id, counters)

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)

Parameters

id req
counters req
Source
# File activerecord/lib/active_record/base.rb, line 982
      def update_counters(id, counters)
        updates = counters.map do |counter_name, value|
          operator = value < 0 ? '-' : '+'
          quoted_column = connection.quote_column_name(counter_name)
          "#{quoted_column} = COALESCE(#{quoted_column}, 0) #{operator} #{value.abs}"
        end

        update_all(updates.join(', '), primary_key => id )
      end

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

Defined in ActiveRecord::Base

Type at least 2 characters to search.

↑↓ navigate · open · esc close