diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2016-07-25 11:06:04 -0400 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2016-07-25 11:12:06 -0400 |
commit | f0ddf87e4bfcfcb861b0a9dca32edb733668bd30 (patch) | |
tree | 37e30078178423e3b7f8850ab7b667f70db9abfc /activerecord/lib/active_record/model_schema.rb | |
parent | 7ebef567ce30d848dd8f9374fd70699fd91acb29 (diff) | |
download | rails-f0ddf87e4bfcfcb861b0a9dca32edb733668bd30.tar.gz rails-f0ddf87e4bfcfcb861b0a9dca32edb733668bd30.tar.bz2 rails-f0ddf87e4bfcfcb861b0a9dca32edb733668bd30.zip |
Correct the behavior of virtual attributes on models loaded from the db
Previously we had primarily tested the behavior of these attributes by
calling `.new`, allowing this to slip through the cracks. There were a
few ways in which they were behaving incorrectly.
The biggest issue was that attempting to read the attribute would
through a `MissingAttribute` error. We've corrected this by returning
the default value when the attribute isn't backed by a database column.
This is super special cased, but I don't see a way to avoid this
conditional. I had considered handling this higher up in
`define_default_attribute`, but we don't have the relevant information
there as users can provide new defaults for database columns as well.
Once I corrected this, I had noticed that the attributes were always
being marked as changed. This is because the behavior of
`define_default_attribute` was treating them as assigned from
`Attribute::Null`.
Finally, with our new implementation, `LazyAttributeHash` could no
longer be marshalled, as it holds onto a proc. This has been corrected
as well. I've not handled YAML in that class, as we do additional work
higher up to avoid YAML dumping it at all.
Fixes #25787
Close #25841
Diffstat (limited to 'activerecord/lib/active_record/model_schema.rb')
-rw-r--r-- | activerecord/lib/active_record/model_schema.rb | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb index 99facb4b25..41cebb0e34 100644 --- a/activerecord/lib/active_record/model_schema.rb +++ b/activerecord/lib/active_record/model_schema.rb @@ -249,7 +249,11 @@ module ActiveRecord end def attributes_builder # :nodoc: - @attributes_builder ||= AttributeSet::Builder.new(attribute_types, primary_key) + @attributes_builder ||= AttributeSet::Builder.new(attribute_types, primary_key) do |name| + unless columns_hash.key?(name) + _default_attributes[name].dup + end + end end def columns_hash # :nodoc: |