提交 74d24ea1 编写于 作者: R Rafael Mendonça França

Merge pull request #9497 from route/subclass_from_attrs

Fix ActiveRecord `subclass_from_attrs` when eager_load is false.

Conflicts:
	activerecord/CHANGELOG.md
## 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 @@ def type_condition(table = arel_table)
# 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 @@ def test_new_with_unrelated_type
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册