mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #9497 from route/subclass_from_attrs
Fix ActiveRecord `subclass_from_attrs` when eager_load is false. Conflicts: activerecord/CHANGELOG.md
This commit is contained in:
commit
74d24ea1fe
4 changed files with 22 additions and 2 deletions
|
@ -1,5 +1,11 @@
|
|||
## Rails 4.0.0 (unreleased) ##
|
||||
|
||||
* Fix ActiveRecord `subclass_from_attrs` when eager_load is false.
|
||||
It cannot find subclass because all classes are loaded automatically
|
||||
when it needs.
|
||||
|
||||
*Dmitry Vorotilin*
|
||||
|
||||
* When `:name` option is provided to `remove_index`, use it if there is no
|
||||
index by the conventional name.
|
||||
|
||||
|
|
|
@ -170,8 +170,9 @@ module ActiveRecord
|
|||
# this will ignore the inheritance column and return nil
|
||||
def subclass_from_attrs(attrs)
|
||||
subclass_name = attrs.with_indifferent_access[inheritance_column]
|
||||
return nil if subclass_name.blank? || subclass_name == self.name
|
||||
unless subclass = subclasses.detect { |sub| sub.name == subclass_name }
|
||||
return if subclass_name.blank? || subclass_name == self.name
|
||||
subclass = subclass_name.safe_constantize
|
||||
unless subclasses.include?(subclass)
|
||||
raise ActiveRecord::SubclassNotFound.new("Invalid single-table inheritance type: #{subclass_name} is not a subclass of #{name}")
|
||||
end
|
||||
subclass
|
||||
|
|
|
@ -179,6 +179,17 @@ class InheritanceTest < ActiveRecord::TestCase
|
|||
assert_raise(ActiveRecord::SubclassNotFound) { Company.new(:type => 'Account') }
|
||||
end
|
||||
|
||||
def test_new_with_autoload_paths
|
||||
path = File.expand_path('../../models/autoloadable', __FILE__)
|
||||
ActiveSupport::Dependencies.autoload_paths << path
|
||||
|
||||
firm = Company.new(:type => 'ExtraFirm')
|
||||
assert_equal ExtraFirm, firm.class
|
||||
ensure
|
||||
ActiveSupport::Dependencies.autoload_paths.reject! { |p| p == path }
|
||||
ActiveSupport::Dependencies.clear
|
||||
end
|
||||
|
||||
def test_inheritance_condition
|
||||
assert_equal 10, Company.count
|
||||
assert_equal 2, Firm.count
|
||||
|
|
2
activerecord/test/models/autoloadable/extra_firm.rb
Normal file
2
activerecord/test/models/autoloadable/extra_firm.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
class ExtraFirm < Company
|
||||
end
|
Loading…
Reference in a new issue