aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorMehmet Emin İNAÇ <mehmetemininac@gmail.com>2015-04-16 17:40:52 +0300
committerMehmet Emin İNAÇ <mehmetemininac@gmail.com>2015-04-19 23:50:13 +0300
commit5e8d96c5234d3f378f9048bf6c0077bb1139ce9a (patch)
treec907b45668c6c48d8dd9c305412e3a1434df0550 /activerecord/lib/active_record
parentc8bab30ce5a7a40be7a3c7c4271e8ae6d2b303bb (diff)
downloadrails-5e8d96c5234d3f378f9048bf6c0077bb1139ce9a.tar.gz
rails-5e8d96c5234d3f378f9048bf6c0077bb1139ce9a.tar.bz2
rails-5e8d96c5234d3f378f9048bf6c0077bb1139ce9a.zip
Raise StaleObjectError if touched object is stale and locking is enabled
Fixes #19776 change test variable names and use more verbose on method less verbose use _read_attribute instead of send
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/persistence.rb18
1 files changed, 15 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index a1e1073792..9bb45aa3b7 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -477,11 +477,23 @@ module ActiveRecord
changes[column] = write_attribute(column, time)
end
- changes[self.class.locking_column] = increment_lock if locking_enabled?
-
clear_attribute_changes(changes.keys)
primary_key = self.class.primary_key
- self.class.unscoped.where(primary_key => self[primary_key]).update_all(changes) == 1
+ scope = self.class.unscoped.where(primary_key => id)
+
+ if locking_enabled?
+ locking_column = self.class.locking_column
+ scope = scope.where(locking_column => _read_attribute(locking_column))
+ changes[locking_column] = increment_lock
+ end
+
+ result = scope.update_all(changes) == 1
+
+ if !result && locking_enabled?
+ raise ActiveRecord::StaleObjectError.new(self, "touch")
+ end
+
+ result
else
true
end