提交 0e113a04 编写于 作者: E Emilio Tagua

Refactored locking update

上级 649fd105
......@@ -90,18 +90,20 @@ def update_with_lock(attribute_names = @attributes.keys) #:nodoc:
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 @@ def destroy_with_lock #:nodoc:
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))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册