From 96a366cd9b52a4e80b0cb36c20f4c0011159d745 Mon Sep 17 00:00:00 2001
From: Aaron Patterson <aaron.patterson@gmail.com>
Date: Tue, 8 Oct 2013 15:46:06 -0700
Subject: cache join associations and join base on the stack

No reason to decompose the join objects on every iteration
---
 .../lib/active_record/associations/join_dependency.rb     | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

(limited to 'activerecord/lib')

diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb
index 8ddfe5742e..4d32fb8571 100644
--- a/activerecord/lib/active_record/associations/join_dependency.rb
+++ b/activerecord/lib/active_record/associations/join_dependency.rb
@@ -62,10 +62,17 @@ module ActiveRecord
       end
 
       def graft(*associations)
-        associations.each do |association|
-          join_associations.detect { |a| association == a } ||
-            find_or_build_scalar(association.reflection.name, find_parent_part(association.parent) || join_base, association.join_type)
-        end
+        join_assocs = join_associations
+        base        = join_base
+
+        associations.reject { |association|
+          join_assocs.detect { |a| association == a }
+        }.each { |association|
+          name      = association.reflection.name
+          join_part = find_parent_part(association.parent) || base
+          type      = association.join_type
+          find_or_build_scalar name, join_part, type
+        }
         self
       end
 
-- 
cgit v1.2.3