aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/model_schema.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2017-11-27 14:06:51 -0700
committerSean Griffin <sean@seantheprogrammer.com>2017-11-27 14:06:51 -0700
commit95b86e57a6c07bc636f8feab4afd4c7fe0ca512c (patch)
treec665e7cd5c61f2c6cd2dae0374838e8c144c98b5 /activerecord/lib/active_record/model_schema.rb
parent924a368f5c654f5304e575c767eb0fc64adc8659 (diff)
downloadrails-95b86e57a6c07bc636f8feab4afd4c7fe0ca512c.tar.gz
rails-95b86e57a6c07bc636f8feab4afd4c7fe0ca512c.tar.bz2
rails-95b86e57a6c07bc636f8feab4afd4c7fe0ca512c.zip
Change how `AttributeSet::Builder` receives its defaults
There are two concerns which are both being combined into one here, but both have the same goal. There are certain attributes which we want to always consider initialized. Previously, they were handled separately. The primary key (which is assumed to be backed by a database column) needs to be initialized, because there is a ton of code in Active Record that assumes `foo.id` will never raise. Additionally, we want attributes which aren't backed by a database column to always be initialized, since we would never receive a database value for them. Ultimately these two concerns can be combined into one. The old implementation hid a lot of inherent complexity, and is hard to optimize from the outside. We can simplify things significantly by just passing in a hash. This has slightly different semantics from the old behavior, in that `Foo.select(:bar).first.id` will return the default value for the primary key, rather than `nil` unconditionally -- however, the default value is always `nil` in practice.
Diffstat (limited to 'activerecord/lib/active_record/model_schema.rb')
-rw-r--r--activerecord/lib/active_record/model_schema.rb8
1 files changed, 4 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index 12ee4a4137..1941d3d5ea 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -323,11 +323,11 @@ module ActiveRecord
end
def attributes_builder # :nodoc:
- @attributes_builder ||= ActiveModel::AttributeSet::Builder.new(attribute_types, primary_key) do |name|
- unless columns_hash.key?(name)
- _default_attributes[name].dup
- end
+ unless defined?(@attributes_builder) && @attributes_builder
+ defaults = _default_attributes.except(*(column_names - [primary_key]))
+ @attributes_builder = ActiveModel::AttributeSet::Builder.new(attribute_types, defaults)
end
+ @attributes_builder
end
def columns_hash # :nodoc: