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:
parent
f396c01db2
commit
8fcd9b6e86
2 changed files with 16 additions and 9 deletions
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue