Skip to content

Commit

Permalink
Make #save return true on success, even if locking is enabled (closes #…
Browse files Browse the repository at this point in the history
…4015) [schoenm@earthlink.net]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3716 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Mar 1, 2006
1 parent 0fa1278 commit 491554f
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions activerecord/lib/active_record/locking.rb
Expand Up @@ -30,24 +30,24 @@ def self.append_features(base) #:nodoc:
end

def update_with_lock #:nodoc:
if locking_enabled?
lock_col = self.class.locking_column
previous_value = send(lock_col)
send(lock_col + '=', previous_value + 1)

affected_rows = connection.update(<<-end_sql, "#{self.class.name} Update with optimistic locking")
UPDATE #{self.class.table_name}
SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))}
WHERE #{self.class.primary_key} = #{quote(id)}
AND #{lock_col} = #{quote(previous_value)}
end_sql
return update_without_lock unless locking_enabled?

unless affected_rows == 1
raise ActiveRecord::StaleObjectError, "Attempted to update a stale object"
end
else
update_without_lock
lock_col = self.class.locking_column
previous_value = send(lock_col)
send(lock_col + '=', previous_value + 1)

affected_rows = connection.update(<<-end_sql, "#{self.class.name} Update with optimistic locking")
UPDATE #{self.class.table_name}
SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))}
WHERE #{self.class.primary_key} = #{quote(id)}
AND #{lock_col} = #{quote(previous_value)}
end_sql

unless affected_rows == 1
raise ActiveRecord::StaleObjectError, "Attempted to update a stale object"
end

return true
end
end

Expand Down

0 comments on commit 491554f

Please sign in to comment.