Skip to content

Commit 97755b8

Browse files
committed
Eliminate lease_connection call in JoinAssociation
1 parent 94faed4 commit 97755b8

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

activerecord/lib/active_record/associations/join_dependency/join_association.rb

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,39 +37,41 @@ def join_constraints(foreign_table, foreign_klass, join_type, alias_tracker)
3737
chain << [reflection, table]
3838
end
3939

40-
# The chain starts with the target table, but we want to end with it here (makes
41-
# more sense in this context), so we reverse
42-
chain.reverse_each do |reflection, table|
43-
klass = reflection.klass
40+
base_klass.with_connection do |connection|
41+
# The chain starts with the target table, but we want to end with it here (makes
42+
# more sense in this context), so we reverse
43+
chain.reverse_each do |reflection, table|
44+
klass = reflection.klass
4445

45-
scope = reflection.join_scope(table, foreign_table, foreign_klass)
46+
scope = reflection.join_scope(table, foreign_table, foreign_klass)
4647

47-
unless scope.references_values.empty?
48-
associations = scope.eager_load_values | scope.includes_values
48+
unless scope.references_values.empty?
49+
associations = scope.eager_load_values | scope.includes_values
4950

50-
unless associations.empty?
51-
scope.joins! scope.construct_join_dependency(associations, Arel::Nodes::OuterJoin)
51+
unless associations.empty?
52+
scope.joins! scope.construct_join_dependency(associations, Arel::Nodes::OuterJoin)
53+
end
5254
end
53-
end
5455

55-
arel = scope.arel(alias_tracker.aliases)
56-
nodes = arel.constraints.first
56+
arel = scope.arel(alias_tracker.aliases)
57+
nodes = arel.constraints.first
5758

58-
if nodes.is_a?(Arel::Nodes::And)
59-
others = nodes.children.extract! do |node|
60-
!Arel.fetch_attribute(node) { |attr| attr.relation.name == table.name }
59+
if nodes.is_a?(Arel::Nodes::And)
60+
others = nodes.children.extract! do |node|
61+
!Arel.fetch_attribute(node) { |attr| attr.relation.name == table.name }
62+
end
6163
end
62-
end
6364

64-
joins << join_type.new(table, Arel::Nodes::On.new(nodes))
65+
joins << join_type.new(table, Arel::Nodes::On.new(nodes))
6566

66-
if others && !others.empty?
67-
joins.concat arel.join_sources
68-
append_constraints(joins.last, others)
69-
end
67+
if others && !others.empty?
68+
joins.concat arel.join_sources
69+
append_constraints(connection, joins.last, others)
70+
end
7071

71-
# The current table in this iteration becomes the foreign table in the next
72-
foreign_table, foreign_klass = table, klass
72+
# The current table in this iteration becomes the foreign table in the next
73+
foreign_table, foreign_klass = table, klass
74+
end
7375
end
7476

7577
joins
@@ -88,10 +90,10 @@ def strict_loading?
8890
end
8991

9092
private
91-
def append_constraints(join, constraints)
93+
def append_constraints(connection, join, constraints)
9294
if join.is_a?(Arel::Nodes::StringJoin)
9395
join_string = Arel::Nodes::And.new(constraints.unshift join.left)
94-
join.left = Arel.sql(base_klass.lease_connection.visitor.compile(join_string))
96+
join.left = Arel.sql(connection.visitor.compile(join_string))
9597
else
9698
right = join.right
9799
right.expr = Arel::Nodes::And.new(constraints.unshift right.expr)

0 commit comments

Comments
 (0)