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

View file

@ -312,6 +312,13 @@ module ActiveRecord
@relation = nil
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
# Guesses the table name, but does not decorate it with prefix and suffix information.