Use const_missing
to autoload associations so we don’t have to require_association when
using single-table inheritance.
# File activesupport/lib/active_support/dependencies.rb, line 175
def const_missing(const_name, nesting = nil)
klass_name = name.presence || "Object"
unless nesting
# We'll assume that the nesting of Foo::Bar is ["Foo::Bar", "Foo"]
# even though it might not be, such as in the case of
# class Foo::Bar; Baz; end
nesting = []
klass_name.to_s.scan(/::|$/) { nesting.unshift $` }
end
# If there are multiple levels of nesting to search under, the top
# level is the one we want to report as the lookup fail.
error = nil
nesting.each do |namespace|
begin
return Dependencies.load_missing_constant Inflector.constantize(namespace), const_name
rescue NoMethodError then raise
rescue NameError => e
error ||= e
end
end
# Raise the first error for this set. If this const_missing came from an
# earlier const_missing, this will result in the real error bubbling
# all the way up
raise error
end