1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

The default value can be set once in #column_defaults

Rather than doing it every time an instance is instantiated.
This commit is contained in:
Jon Leighton 2012-08-17 16:38:55 +01:00
parent f396c01db2
commit 8fcd9b6e86
2 changed files with 16 additions and 9 deletions

View file

@ -168,16 +168,16 @@ module ActiveRecord
super super
end end
# If the locking column has no default value set, def column_defaults
# start the lock version at zero. Note we can't use @column_defaults ||= begin
# <tt>locking_enabled?</tt> at this point as defaults = super
# <tt>@attributes</tt> may not have been initialized yet.
def initialize_attributes(attributes, options = {}) #:nodoc: if defaults.key?(locking_column) && lock_optimistically
if attributes.key?(locking_column) && lock_optimistically defaults[locking_column] ||= 0
attributes[locking_column] ||= 0
end end
attributes defaults
end
end end
end end
end end

View file

@ -312,6 +312,13 @@ module ActiveRecord
@relation = nil @relation = nil
end end
# This is a hook for use by modules that need to do extra stuff to
# attributes when they are initialized. (e.g. attribute
# serialization)
def initialize_attributes(attributes, options = {}) #:nodoc:
attributes
end
private private
# Guesses the table name, but does not decorate it with prefix and suffix information. # Guesses the table name, but does not decorate it with prefix and suffix information.