diff options
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/locking/optimistic.rb | 17 |
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? |