From 0e113a040d934958ce3805ce6cda73c655def444 Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Fri, 24 Apr 2009 17:32:16 -0300 Subject: Refactored locking update --- activerecord/lib/active_record/locking/optimistic.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'activerecord/lib/active_record/locking/optimistic.rb') diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index ab0f5fc58d..1251e9f013 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -90,18 +90,20 @@ module ActiveRecord begin table = Arel(self.class.table_name) - affected_rows = table.where( - table[self.class.primary_key].eq(quoted_id).and( - table[self.class.locking_column].eq(quote_value(previous_value)) - ) - ) attributes = {} attributes_with_quotes(false, false, attribute_names).map { |k,v| attributes.merge!(table[k] => v) } - unless affected_rows.update(attributes) == 1 + affected_rows = table.where( + table[self.class.primary_key].eq(quoted_id).and( + table[self.class.locking_column].eq(quote_value(previous_value)) + ) + ).update(attributes) + + + unless affected_rows == 1 raise ActiveRecord::StaleObjectError, "Attempted to update a stale object" end @@ -121,7 +123,7 @@ module ActiveRecord lock_col = self.class.locking_column previous_value = send(lock_col).to_i - table = Arel(self.class.table_name, connection) + table = Arel(self.class.table_name) affected_rows = table.where( table[self.class.primary_key].eq(quoted_id).and( table[self.class.locking_column].eq(quote_value(previous_value)) -- cgit v1.2.3