Cache the constants in @@subclasses because looking them up via const_get
make instantiation significantly slower.
# File activerecord/lib/active_record/relation/delegation.rb, line 91
def relation_class_for(klass)
if klass && (klass_name = klass.name)
my_cache = @@subclasses.compute_if_absent(self) { ThreadSafe::Cache.new }
# This hash is keyed by klass.name to avoid memory leaks in development mode
my_cache.compute_if_absent(klass_name) do
# Cache#compute_if_absent guarantees that the block will only executed once for the given klass_name
const_get("#{name.gsub('::', '_')}_#{klass_name.gsub('::', '_')}", false)
end
else
ActiveRecord::Relation
end
end