diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-12-26 17:36:47 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-12-26 17:36:47 -0700 |
commit | 15e2d5043f181a480f9bb3c3ff19b03505e7c4a1 (patch) | |
tree | 243109ab506906617b4f4ca08c7d36dda36bd1f2 /activerecord/lib/active_record/locking | |
parent | 0520e6559ef377de841c2fb2345835732756f451 (diff) | |
download | rails-15e2d5043f181a480f9bb3c3ff19b03505e7c4a1.tar.gz rails-15e2d5043f181a480f9bb3c3ff19b03505e7c4a1.tar.bz2 rails-15e2d5043f181a480f9bb3c3ff19b03505e7c4a1.zip |
Go through normal `update_all` logic when updating lock columns
Diffstat (limited to 'activerecord/lib/active_record/locking')
-rw-r--r-- | activerecord/lib/active_record/locking/optimistic.rb | 24 |
1 files changed, 7 insertions, 17 deletions
diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index eb0bcff740..7bca38c910 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -80,24 +80,14 @@ module ActiveRecord begin relation = self.class.unscoped - # FIXME: Remove the Arel::Nodes::Quoted when we remove type casting - # from Arel (Rails 5.1) - quoted_lock_value = Arel::Nodes::Quoted.new( - self.class.quote_value( - previous_lock_value, - column_for_attribute(lock_col), - ) + affected_rows = relation.where( + self.class.primary_key => id, + lock_col => previous_lock_value, + ).update_all( + attribute_names.map do |name| + [name, _read_attribute(name)] + end.to_h ) - quoted_id = Arel::Nodes::Quoted.new(id) - stmt = relation.where( - relation.table[self.class.primary_key].eq(quoted_id).and( - relation.table[lock_col].eq(quoted_lock_value)) - ).arel.compile_update( - arel_attributes_with_values_for_update(attribute_names), - self.class.primary_key - ) - - affected_rows = self.class.connection.update stmt unless affected_rows == 1 raise ActiveRecord::StaleObjectError.new(self, "update") |