aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/persistence.rb
diff options
context:
space:
mode:
authorBogdan Gusiev <agresso@gmail.com>2015-10-03 16:36:49 +0300
committerBogdan Gusiev <agresso@gmail.com>2015-10-05 15:13:04 +0300
commit85a1c025081faf8af8a065e39ee764caebf4054b (patch)
tree64039ea2d163272044d01a96b4ff85c361512fb3 /activerecord/lib/active_record/persistence.rb
parent210f33c477e950ad44e66b47da794469ce3b5e64 (diff)
downloadrails-85a1c025081faf8af8a065e39ee764caebf4054b.tar.gz
rails-85a1c025081faf8af8a065e39ee764caebf4054b.tar.bz2
rails-85a1c025081faf8af8a065e39ee764caebf4054b.zip
Make #increment! and #decrement! methods concurency safe
Diffstat (limited to 'activerecord/lib/active_record/persistence.rb')
-rw-r--r--activerecord/lib/active_record/persistence.rb12
1 files changed, 7 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index 7b53f6e5a0..39e6057b9c 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -332,16 +332,18 @@ module ActiveRecord
# Saving is not subjected to validation checks. Returns +true+ if the
# record could be saved.
def increment!(attribute, by = 1)
- increment(attribute, by).update_attribute(attribute, self[attribute])
+ increment(attribute, by)
+ change = public_send(attribute) - (attribute_was(attribute.to_s) || 0)
+ self.class.update_counters(id, attribute => change)
+ clear_attribute_change(attribute) # eww
+ self
end
# Initializes +attribute+ to zero if +nil+ and subtracts the value passed as +by+ (default is 1).
# The decrement is performed directly on the underlying attribute, no setter is invoked.
# Only makes sense for number-based attributes. Returns +self+.
def decrement(attribute, by = 1)
- self[attribute] ||= 0
- self[attribute] -= by
- self
+ increment(attribute, -by)
end
# Wrapper around +decrement+ that saves the record. This method differs from
@@ -349,7 +351,7 @@ module ActiveRecord
# Saving is not subjected to validation checks. Returns +true+ if the
# record could be saved.
def decrement!(attribute, by = 1)
- decrement(attribute, by).update_attribute(attribute, self[attribute])
+ increment!(attribute, -by)
end
# Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So