diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-12-11 14:53:35 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-12-13 14:13:52 +0900 |
commit | d4007d5a54b60bd6924eeffb52c126ed32e9f31f (patch) | |
tree | af37d31ea93a287efe7cb49b8aaef3bf8d001d57 /activerecord/lib/active_record/inheritance.rb | |
parent | f5735ff7d470c2174f06407008e9809d8d9c1574 (diff) | |
download | rails-d4007d5a54b60bd6924eeffb52c126ed32e9f31f.tar.gz rails-d4007d5a54b60bd6924eeffb52c126ed32e9f31f.tar.bz2 rails-d4007d5a54b60bd6924eeffb52c126ed32e9f31f.zip |
Fix inheritance object creation from relation
We need to pass scope attributes to `klass.new` to detect subclass.
Otherwise `subclass_from_attributes` can't detect subclass which is had
in scope attributes.
Fixes #18062.
Closes #18227.
Closes #30720.
Diffstat (limited to 'activerecord/lib/active_record/inheritance.rb')
-rw-r--r-- | activerecord/lib/active_record/inheritance.rb | 7 |
1 files changed, 3 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb index 0715c11473..f3fe610c09 100644 --- a/activerecord/lib/active_record/inheritance.rb +++ b/activerecord/lib/active_record/inheritance.rb @@ -47,14 +47,13 @@ module ActiveRecord # Determines if one of the attributes passed in is the inheritance column, # and if the inheritance column is attr accessible, it initializes an # instance of the given subclass instead of the base class. - def new(*args, &block) + def new(attributes = nil, &block) if abstract_class? || self == Base raise NotImplementedError, "#{self} is an abstract class and cannot be instantiated." end - attrs = args.first if has_attribute?(inheritance_column) - subclass = subclass_from_attributes(attrs) + subclass = subclass_from_attributes(attributes) if subclass.nil? && base_class == self subclass = subclass_from_attributes(column_defaults) @@ -62,7 +61,7 @@ module ActiveRecord end if subclass && subclass != self - subclass.new(*args, &block) + subclass.new(attributes, &block) else super end |