aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/inheritance.rb
diff options
context:
space:
mode:
authorUjjwal Thaakar <ujjwalthaakar@gmail.com>2014-01-14 18:53:45 +0530
committerUjjwal Thaakar <ujjwalthaakar@gmail.com>2014-01-14 18:53:45 +0530
commite8d1d84837a59ef7d73b29b16ee05cd610d30a90 (patch)
treecc13fee530ff0a6045b8a4acb58e9a96e4b818cc /activerecord/lib/active_record/inheritance.rb
parentb242b2dbe75f0b5e86e2ce9ef7c2c5ee96e17862 (diff)
downloadrails-e8d1d84837a59ef7d73b29b16ee05cd610d30a90.tar.gz
rails-e8d1d84837a59ef7d73b29b16ee05cd610d30a90.tar.bz2
rails-e8d1d84837a59ef7d73b29b16ee05cd610d30a90.zip
Don't try to get the subclass if the inheritance column doesn't exist
The `subclass_from_attrs` method is called even if the column specified by the `inheritance_column` setting doesn't exist. This prevents setting associations via the attributes hash if the association name clashes with the value of the setting, typically `:type`. This worked previously in Rails 3.2.
Diffstat (limited to 'activerecord/lib/active_record/inheritance.rb')
-rw-r--r--activerecord/lib/active_record/inheritance.rb22
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