aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-02-06 10:45:06 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-02-06 22:32:03 +0900
commit06d04bc237600c9a524e45609280c1b46c271e18 (patch)
tree86c401e837931bcbc5101a86924f15093b3eff31 /activerecord
parent83c1ed9a1a11196cab66d9c44a56a902ca0710e4 (diff)
downloadrails-06d04bc237600c9a524e45609280c1b46c271e18.tar.gz
rails-06d04bc237600c9a524e45609280c1b46c271e18.tar.bz2
rails-06d04bc237600c9a524e45609280c1b46c271e18.zip
Invoke `load_schema` in `_default_attributes`
Currently `_default_attributes` doesn't work unless `load_schema` is called before. The `MissingAttributeError` is caused by `reload_schema_from_cache` is invoked by `serialize`. I added `load_schema` in `_default_attributes` to `_default_attributes` works without any dependency like `attribute_types` etc. Closes #31905.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/model_schema.rb1
-rw-r--r--activerecord/test/cases/persistence_test.rb7
2 files changed, 8 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index 60823815fa..b04dc04899 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -379,6 +379,7 @@ module ActiveRecord
end
def _default_attributes # :nodoc:
+ load_schema
@default_attributes ||= ActiveModel::AttributeSet.new({})
end
diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb
index e8052914d4..d4a4cd564a 100644
--- a/activerecord/test/cases/persistence_test.rb
+++ b/activerecord/test/cases/persistence_test.rb
@@ -251,6 +251,13 @@ class PersistenceTest < ActiveRecord::TestCase
assert_equal "The First Topic", topics(:first).becomes(Reply).title
end
+ def test_becomes_after_reload_schema_from_cache
+ Reply.define_attribute_methods
+ Reply.serialize(:content) # invoke reload_schema_from_cache
+ assert_kind_of Reply, topics(:first).becomes(Reply)
+ assert_equal "The First Topic", topics(:first).becomes(Reply).title
+ end
+
def test_becomes_includes_errors
company = Company.new(name: nil)
assert_not_predicate company, :valid?