From 6b18bdd00ce1e8eb4a5a30f0f6152e47da9bec42 Mon Sep 17 00:00:00 2001 From: Kuldeep Aggarwal Date: Wed, 2 Dec 2015 16:08:19 +0530 Subject: STI cast new instances to `default type` on initialize. fixes #17121 --- activerecord/test/cases/inheritance_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index 52e3734dd0..e7709c04c8 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -478,4 +478,24 @@ class InheritanceComputeTypeTest < ActiveRecord::TestCase product = Shop::Product.new(:type => phone) assert product.save end + + def test_inheritance_new_with_subclass_as_default + original_type = Company.columns_hash["type"].default + ActiveRecord::Base.connection.change_column_default :companies, :type, 'Firm' + Company.reset_column_information + # this is the case when attrs is a +Hash+, but we didn't specify the type, + # so we need default type. + firm = Company.new(firm_name: 'Shri Hans Plastic') + assert_equal 'Firm', firm.type + assert_instance_of Firm, firm + firm = Company.new # this is the case when attrs is nil + assert_equal 'Firm', firm.type + assert_instance_of Firm, firm + firm = Company.new(type: 'Client') + assert_equal 'Client', firm.type + assert_instance_of Client, firm + ensure + ActiveRecord::Base.connection.change_column_default :companies, :type, original_type + Company.reset_column_information + end end -- cgit v1.2.3