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/lib/active_record/inheritance.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb index 8b719e0bcb..ed95b30f80 100644 --- a/activerecord/lib/active_record/inheritance.rb +++ b/activerecord/lib/active_record/inheritance.rb @@ -55,6 +55,8 @@ module ActiveRecord subclass = subclass_from_attributes(attrs) end + subclass ||= subclass_from_defaults + if subclass && subclass != self subclass.new(*args, &block) else @@ -201,6 +203,16 @@ module ActiveRecord attribute_names.include?(inheritance_column) && (attrs.is_a?(Hash) || attrs.respond_to?(:permitted?)) end + def subclass_from_defaults? + attribute_names.include?(inheritance_column) && columns_hash[inheritance_column].try(:default) + end + + def subclass_from_defaults + if subclass_from_defaults? + find_sti_class(columns_hash[inheritance_column].default) + end + end + def subclass_from_attributes(attrs) attrs = attrs.to_h if attrs.respond_to?(:permitted?) subclass_name = attrs.with_indifferent_access[inheritance_column] -- cgit v1.2.3