File tree Expand file tree Collapse file tree 4 files changed +22
-2
lines changed Expand file tree Collapse file tree 4 files changed +22
-2
lines changed Original file line number Diff line number Diff line change 1
1
## Rails 4.0.0 (unreleased) ##
2
2
3
+ * Fix ActiveRecord ` subclass_from_attrs ` when eager_load is false.
4
+ It cannot find subclass because all classes are loaded automatically
5
+ when it needs.
6
+
7
+ * Dmitry Vorotilin*
8
+
3
9
* When ` :name ` option is provided to ` remove_index ` , use it if there is no
4
10
index by the conventional name.
5
11
Original file line number Diff line number Diff line change @@ -170,8 +170,9 @@ def type_condition(table = arel_table)
170
170
# this will ignore the inheritance column and return nil
171
171
def subclass_from_attrs ( attrs )
172
172
subclass_name = attrs . with_indifferent_access [ inheritance_column ]
173
- return nil if subclass_name . blank? || subclass_name == self . name
174
- unless subclass = subclasses . detect { |sub | sub . name == subclass_name }
173
+ return if subclass_name . blank? || subclass_name == self . name
174
+ subclass = subclass_name . safe_constantize
175
+ unless subclasses . include? ( subclass )
175
176
raise ActiveRecord ::SubclassNotFound . new ( "Invalid single-table inheritance type: #{ subclass_name } is not a subclass of #{ name } " )
176
177
end
177
178
subclass
Original file line number Diff line number Diff line change @@ -179,6 +179,17 @@ def test_new_with_unrelated_type
179
179
assert_raise ( ActiveRecord ::SubclassNotFound ) { Company . new ( :type => 'Account' ) }
180
180
end
181
181
182
+ def test_new_with_autoload_paths
183
+ path = File . expand_path ( '../../models/autoloadable' , __FILE__ )
184
+ ActiveSupport ::Dependencies . autoload_paths << path
185
+
186
+ firm = Company . new ( :type => 'ExtraFirm' )
187
+ assert_equal ExtraFirm , firm . class
188
+ ensure
189
+ ActiveSupport ::Dependencies . autoload_paths . reject! { |p | p == path }
190
+ ActiveSupport ::Dependencies . clear
191
+ end
192
+
182
193
def test_inheritance_condition
183
194
assert_equal 10 , Company . count
184
195
assert_equal 2 , Firm . count
Original file line number Diff line number Diff line change
1
+ class ExtraFirm < Company
2
+ end
You can’t perform that action at this time.
0 commit comments