aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/locking
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-09-04 00:02:38 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-09-04 00:02:38 +0000
commit528618a91082748133488fefbf5f57cf8b96ed75 (patch)
treea560639cb12de229e868d657dea678c33862badf /activerecord/lib/active_record/locking
parent9b18c1cb5768b0777b16bccdbfc3891a86af4558 (diff)
downloadrails-528618a91082748133488fefbf5f57cf8b96ed75.tar.gz
rails-528618a91082748133488fefbf5f57cf8b96ed75.tar.bz2
rails-528618a91082748133488fefbf5f57cf8b96ed75.zip
Optimistic locking: gracefully handle nil versions, treat as zero. Closes #5908.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4958 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/locking')
-rw-r--r--activerecord/lib/active_record/locking/optimistic.rb17
1 files changed, 17 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb
index 7bae573924..823f4e19fc 100644
--- a/activerecord/lib/active_record/locking/optimistic.rb
+++ b/activerecord/lib/active_record/locking/optimistic.rb
@@ -30,6 +30,8 @@ module ActiveRecord
base.lock_optimistically = true
base.alias_method_chain :update, :lock
+ base.alias_method_chain :attributes_from_column_definition, :lock
+
class << base
alias_method :locking_column=, :set_locking_column
end
@@ -39,6 +41,21 @@ module ActiveRecord
lock_optimistically && respond_to?(self.class.locking_column)
end
+ def attributes_from_column_definition_with_lock
+ result = attributes_from_column_definition_without_lock
+
+ # If the locking column has no default value set,
+ # start the lock version at zero. Note we can't use
+ # locking_enabled? at this point as @attributes may
+ # not have been initialized yet
+
+ if lock_optimistically && result.include?(self.class.locking_column)
+ result[self.class.locking_column] ||= 0
+ end
+
+ return result
+ end
+
def update_with_lock #:nodoc:
return update_without_lock unless locking_enabled?