diff options
author | Dmitry Vorotilin <d.vorotilin@gmail.com> | 2013-03-01 02:32:38 +0400 |
---|---|---|
committer | Dmitry Vorotilin <d.vorotilin@gmail.com> | 2013-03-06 11:46:07 +0400 |
commit | b04051d4e0c8066ec79cd4978606e4728dfa6ffa (patch) | |
tree | f5e3078bf8bfc31de0e65541489b3294642ab772 | |
parent | ee169329a898d993f0d024af6cce83649fb2f733 (diff) | |
download | rails-b04051d4e0c8066ec79cd4978606e4728dfa6ffa.tar.gz rails-b04051d4e0c8066ec79cd4978606e4728dfa6ffa.tar.bz2 rails-b04051d4e0c8066ec79cd4978606e4728dfa6ffa.zip |
Fix ActiveRecord `subclass_from_attrs` when eager_load is false.
It cannot find subclass because all classes are loaded automatically
when it needs.
-rw-r--r-- | activerecord/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/inheritance.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/inheritance_test.rb | 11 | ||||
-rw-r--r-- | activerecord/test/models/autoloadable/extra_firm.rb | 2 |
4 files changed, 22 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 97616ffc58..97e81de1ca 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -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* + * Assigning "0.0" to a nullable numeric column does not make it dirty. Fix #9034. diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb index e630897a4b..e4586f74fc 100644 --- a/activerecord/lib/active_record/inheritance.rb +++ b/activerecord/lib/active_record/inheritance.rb @@ -167,8 +167,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 diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index 189066eb41..b91146db4e 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -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 diff --git a/activerecord/test/models/autoloadable/extra_firm.rb b/activerecord/test/models/autoloadable/extra_firm.rb new file mode 100644 index 0000000000..5578ba0d9b --- /dev/null +++ b/activerecord/test/models/autoloadable/extra_firm.rb @@ -0,0 +1,2 @@ +class ExtraFirm < Company +end |