diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-04-19 16:54:39 -0400 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-04-19 16:54:39 -0400 |
commit | 9721b45b40c045a132b7177f0e86b0f5d567a2df (patch) | |
tree | 82ab33b69395bc189a4032ffd0bdba3833c958c9 /activerecord/lib/active_record | |
parent | 8f3584ab30cb1d2a23a2c59cd0c2e467facd390d (diff) | |
parent | 5e8d96c5234d3f378f9048bf6c0077bb1139ce9a (diff) | |
download | rails-9721b45b40c045a132b7177f0e86b0f5d567a2df.tar.gz rails-9721b45b40c045a132b7177f0e86b0f5d567a2df.tar.bz2 rails-9721b45b40c045a132b7177f0e86b0f5d567a2df.zip |
Merge pull request #19783 from vngrs/raise_error_on_touch_if_object_is_stale
Raise StaleObjectError if touched object is stale and locking is enabled
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 18 |
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 |