diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-11-07 08:17:25 -0700 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-11-07 08:20:34 -0700 |
commit | 9deb6ababe87393fb7806c5097faf51f2852deda (patch) | |
tree | eace414b21e78b670d92447d3ea44993463a711d | |
parent | d238cb88a33a9fb2b53f63ce28d38a3e1a6a1ead (diff) | |
download | rails-9deb6ababe87393fb7806c5097faf51f2852deda.tar.gz rails-9deb6ababe87393fb7806c5097faf51f2852deda.tar.bz2 rails-9deb6ababe87393fb7806c5097faf51f2852deda.zip |
Ensure `#reset_column_information` clears child classes as well
I've added a redundant test for this under the attributes API as well,
as that also causes this bug to manifest through public API (and
demonstrates that calling `reset_column_information` on the child
classes would be insufficient)
Since children of a class should always share a table with their parent,
just reloading the schema from the cache should be sufficient here.
`reload_schema_from_cache` should probably become public and
`# :nodoc:`, but I'd rather avoid the git churn here.
Fixes #22057
-rw-r--r-- | activerecord/lib/active_record/model_schema.rb | 3 | ||||
-rw-r--r-- | activerecord/test/cases/attributes_test.rb | 13 | ||||
-rw-r--r-- | activerecord/test/cases/persistence_test.rb | 12 |
3 files changed, 28 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb index a9bd094a66..e3f304b0af 100644 --- a/activerecord/lib/active_record/model_schema.rb +++ b/activerecord/lib/active_record/model_schema.rb @@ -339,6 +339,9 @@ module ActiveRecord @columns = nil @columns_hash = nil @attribute_names = nil + direct_descendants.each do |descendant| + descendant.send(:reload_schema_from_cache) + end end # Guesses the table name, but does not decorate it with prefix and suffix information. diff --git a/activerecord/test/cases/attributes_test.rb b/activerecord/test/cases/attributes_test.rb index 264b275181..2991ca8b76 100644 --- a/activerecord/test/cases/attributes_test.rb +++ b/activerecord/test/cases/attributes_test.rb @@ -172,5 +172,18 @@ module ActiveRecord assert_equal int_range, klass.type_for_attribute("my_int_range") end end + + test "attributes added after subclasses load are inherited" do + parent = Class.new(ActiveRecord::Base) do + self.table_name = "topics" + end + + child = Class.new(parent) + child.new # => force a schema load + + parent.attribute(:foo, Type::Value.new) + + assert_equal(:bar, child.new(foo: :bar).foo) + end end end diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index acc3103ac6..7e3b226805 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -966,4 +966,16 @@ class PersistenceTest < ActiveRecord::TestCase widget.reset_column_information end end + + def test_reset_column_information_resets_children + child = Class.new(Topic) + child.new # force schema to load + + ActiveRecord::Base.connection.add_column(:topics, :foo, :string) + Topic.reset_column_information + + assert_equal "bar", child.new(foo: :bar).foo + ensure + ActiveRecord::Base.connection.remove_column(:topics, :foo) + end end |