aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2016-01-27 13:26:20 -0500
committerSean Griffin <sean@seantheprogrammer.com>2016-01-27 13:26:20 -0500
commit77383fc1e473819971d1e3ca614d7b361fa5cc33 (patch)
tree6e81f97075d4aef0083b9c2f8249ba24cdf78510
parent9864bcee8cfdb9e798473e01936b4499e268a64d (diff)
downloadrails-77383fc1e473819971d1e3ca614d7b361fa5cc33.tar.gz
rails-77383fc1e473819971d1e3ca614d7b361fa5cc33.tar.bz2
rails-77383fc1e473819971d1e3ca614d7b361fa5cc33.zip
Do not use default attributes for STI when instantiating a subclass
The commit which originally added this behavior did not consider that doing `Subclass.new` does not actually populate the `type` field in the attributes (though perhaps it should). We simply need to not use the defaults for STI related things unless we are instantiating the base class. Fixes #23285.
-rw-r--r--activerecord/lib/active_record/inheritance.rb6
-rw-r--r--activerecord/test/cases/inheritance_test.rb4
2 files changed, 9 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb
index 3a17f74b1d..3b6fb70d0d 100644
--- a/activerecord/lib/active_record/inheritance.rb
+++ b/activerecord/lib/active_record/inheritance.rb
@@ -52,7 +52,11 @@ module ActiveRecord
attrs = args.first
if has_attribute?(inheritance_column)
- subclass = subclass_from_attributes(attrs) || subclass_from_attributes(column_defaults)
+ subclass = subclass_from_attributes(attrs)
+
+ if subclass.nil? && base_class == self
+ subclass = subclass_from_attributes(column_defaults)
+ end
end
if subclass && subclass != self
diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb
index c870247a4a..7da6842047 100644
--- a/activerecord/test/cases/inheritance_test.rb
+++ b/activerecord/test/cases/inheritance_test.rb
@@ -493,6 +493,10 @@ class InheritanceComputeTypeTest < ActiveRecord::TestCase
assert_equal 'Firm', firm.type
assert_instance_of Firm, firm
+ client = Client.new
+ assert_equal 'Client', client.type
+ assert_instance_of Client, client
+
firm = Company.new(type: 'Client') # overwrite the default type
assert_equal 'Client', firm.type
assert_instance_of Client, firm