From 722319d2fe75204458ab97d5b9b1e28c7f0b1e18 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 4 Mar 2016 09:43:35 -0800 Subject: don't build the reflection chain to calculate `nested?` We know a reflection can be considered as `nested?` if the source reflection or the through reflection are also through reflections (since the through reflection will also add another join table). This allows us to avoid traversing the entire reflection tree just to calculate whether or not there will be more join tables. --- activerecord/lib/active_record/reflection.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 0eff30f1db..2ca3206f20 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -138,6 +138,10 @@ module ActiveRecord # PolymorphicReflection # RuntimeReflection class AbstractReflection # :nodoc: + def through_reflection? + false + end + def table_name klass.table_name end @@ -700,6 +704,10 @@ module ActiveRecord @source_reflection_name = delegate_reflection.options[:source] end + def through_reflection? + true + end + def klass @klass ||= delegate_reflection.compute_class(class_name) end @@ -817,7 +825,7 @@ module ActiveRecord # A through association is nested if there would be more than one join table def nested? - chain.length > 2 + source_reflection.through_reflection? || through_reflection.through_reflection? end # We want to use the klass from this reflection, rather than just delegate straight to -- cgit v1.2.3