diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2014-01-14 07:05:54 -0800 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2014-01-14 07:05:54 -0800 |
commit | 8a60f479b0580e88bae77c0ab3dd9e95082cb437 (patch) | |
tree | cc13fee530ff0a6045b8a4acb58e9a96e4b818cc /activerecord/lib/active_record/inheritance.rb | |
parent | b242b2dbe75f0b5e86e2ce9ef7c2c5ee96e17862 (diff) | |
parent | e8d1d84837a59ef7d73b29b16ee05cd610d30a90 (diff) | |
download | rails-8a60f479b0580e88bae77c0ab3dd9e95082cb437.tar.gz rails-8a60f479b0580e88bae77c0ab3dd9e95082cb437.tar.bz2 rails-8a60f479b0580e88bae77c0ab3dd9e95082cb437.zip |
Merge pull request #13710 from ujjwalt/hotfix/sti
Active Record's subclass_from_attributes shouldn't assume :type is for STI unless there is a type column.
Diffstat (limited to 'activerecord/lib/active_record/inheritance.rb')
-rw-r--r-- | activerecord/lib/active_record/inheritance.rb | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb index 69896f7219..da73112e90 100644 --- a/activerecord/lib/active_record/inheritance.rb +++ b/activerecord/lib/active_record/inheritance.rb @@ -18,13 +18,17 @@ module ActiveRecord if abstract_class? || self == Base raise NotImplementedError, "#{self} is an abstract class and cannot be instantiated." end - if (attrs = args.first).is_a?(Hash) - if subclass = subclass_from_attrs(attrs) - return subclass.new(*args, &block) - end + + attrs = args.first + if subclass_from_attributes?(attrs) + subclass = subclass_from_attributes(attrs) + end + + if subclass + subclass.new(*args, &block) + else + super end - # Delegate to the original .new - super end # Returns +true+ if this does not need STI type condition. Returns @@ -172,7 +176,11 @@ module ActiveRecord # is not self or a valid subclass, raises ActiveRecord::SubclassNotFound # If this is a StrongParameters hash, and access to inheritance_column is not permitted, # this will ignore the inheritance column and return nil - def subclass_from_attrs(attrs) + def subclass_from_attributes?(attrs) + columns_hash.include?(inheritance_column) && attrs.is_a?(Hash) + end + + def subclass_from_attributes(attrs) subclass_name = attrs.with_indifferent_access[inheritance_column] if subclass_name.present? && subclass_name != self.name |