From a64f1ea08cf9ce38a920c247866804e9449ba7da Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 9 Oct 2013 15:08:22 -0700 Subject: just skip the join if it's already there Remove the annoying `find_or_build` stuff. --- .../active_record/associations/join_dependency.rb | 24 +++++++++------------- 1 file changed, 10 insertions(+), 14 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 199d28426b..4f30d71b98 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -70,10 +70,13 @@ module ActiveRecord associations.reject { |association| join_assocs.detect { |a| node_cmp association, a } - }.each { |association| - join_node = find_node(association.parent) || @join_root - type = association.join_type - find_or_build_scalar association.reflection, join_node, type + }.each { |join_node| + parent = find_node(join_node.parent) || @join_root + reflection = join_node.reflection + type = join_node.join_type + + next if parent.children.find { |j| j.reflection == reflection } + build_scalar reflection, parent, type } self end @@ -194,16 +197,9 @@ module ActiveRecord end end - def find_or_build_scalar(reflection, parent, join_type) - unless join_association = find_join_association(reflection, parent) - join_association = build_join_association(reflection, parent, join_type) - parent.children << join_association - end - join_association - end - - def find_join_association(reflection, parent) - parent.children.find { |j| j.reflection == reflection } + def build_scalar(reflection, parent, join_type) + join_association = build_join_association(reflection, parent, join_type) + parent.children << join_association end def remove_uniq_by_reflection(reflection, records) -- cgit v1.2.3