method
update_counters
v2.0.3 -
Show latest stable
-
0 notes -
Class: ActiveRecord::Base
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3 (0)
- 2.1.0 (-1)
- 2.2.1 (0)
- 2.3.8 (9)
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
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:
Options
id The id of the object you wish to update a counter on counters An <a href="/rails/Array">Array</a> 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