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

Evaluate the default block only when necessary

Follow up of #28453.
This commit is contained in:
Ryuta Kamizono 2017-03-20 00:17:37 +09:00
parent cf4f05a7d4
commit 4c1d22183c
2 changed files with 3 additions and 3 deletions

View file

@ -21,8 +21,8 @@ module ActiveRecord
self.target = record
end
def default(record)
writer(record) if reader.nil?
def default(&block)
writer(instance_exec(&block)) if reader.nil?
end
def reset

View file

@ -121,7 +121,7 @@ module ActiveRecord::Associations::Builder # :nodoc:
def self.add_default_callbacks(model, reflection)
model.before_validation lambda { |o|
o.association(reflection.name).default o.instance_exec(&reflection.options[:default])
o.association(reflection.name).default(&reflection.options[:default])
}
end