@@ -126,6 +126,7 @@ def update_counters(id, counters)
126
126
#
127
127
# * +counter_name+ - The name of the field that should be incremented.
128
128
# * +id+ - The id of the object that should be incremented or an array of ids.
129
+ # * <tt>:by</tt> - The amount by which to increment the value. Defaults to +1+.
129
130
# * <tt>:touch</tt> - Touch timestamp columns when updating.
130
131
# Pass +true+ to touch +updated_at+ and/or +updated_on+. Pass a symbol to
131
132
# touch that column or an array of symbols to touch just those ones.
@@ -136,10 +137,14 @@ def update_counters(id, counters)
136
137
# DiscussionBoard.increment_counter(:posts_count, 5)
137
138
#
138
139
# # Increment the posts_count column for the record with an id of 5
140
+ # # by a specific amount.
141
+ # DiscussionBoard.increment_counter(:posts_count, 5, by: 3)
142
+ #
143
+ # # Increment the posts_count column for the record with an id of 5
139
144
# # and update the updated_at value.
140
145
# DiscussionBoard.increment_counter(:posts_count, 5, touch: true)
141
- def increment_counter ( counter_name , id , touch : nil )
142
- update_counters ( id , counter_name => 1 , touch : touch )
146
+ def increment_counter ( counter_name , id , by : 1 , touch : nil )
147
+ update_counters ( id , counter_name => by , touch : touch )
143
148
end
144
149
145
150
# Decrement a numeric field by one, via a direct SQL update.
@@ -151,6 +156,7 @@ def increment_counter(counter_name, id, touch: nil)
151
156
#
152
157
# * +counter_name+ - The name of the field that should be decremented.
153
158
# * +id+ - The id of the object that should be decremented or an array of ids.
159
+ # * <tt>:by</tt> - The amount by which to increment the value. Defaults to +1+.
154
160
# * <tt>:touch</tt> - Touch timestamp columns when updating.
155
161
# Pass +true+ to touch +updated_at+ and/or +updated_on+. Pass a symbol to
156
162
# touch that column or an array of symbols to touch just those ones.
@@ -161,10 +167,14 @@ def increment_counter(counter_name, id, touch: nil)
161
167
# DiscussionBoard.decrement_counter(:posts_count, 5)
162
168
#
163
169
# # Decrement the posts_count column for the record with an id of 5
170
+ # by a specific amount.
171
+ # DiscussionBoard.decrement_counter(:posts_count, 5, by: 3)
172
+ #
173
+ # # Decrement the posts_count column for the record with an id of 5
164
174
# # and update the updated_at value.
165
175
# DiscussionBoard.decrement_counter(:posts_count, 5, touch: true)
166
- def decrement_counter ( counter_name , id , touch : nil )
167
- update_counters ( id , counter_name => -1 , touch : touch )
176
+ def decrement_counter ( counter_name , id , by : 1 , touch : nil )
177
+ update_counters ( id , counter_name => -by , touch : touch )
168
178
end
169
179
170
180
def counter_cache_column? ( name ) # :nodoc:
0 commit comments