@@ -37,39 +37,41 @@ def join_constraints(foreign_table, foreign_klass, join_type, alias_tracker)
37
37
chain << [ reflection , table ]
38
38
end
39
39
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
44
45
45
- scope = reflection . join_scope ( table , foreign_table , foreign_klass )
46
+ scope = reflection . join_scope ( table , foreign_table , foreign_klass )
46
47
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
49
50
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
52
54
end
53
- end
54
55
55
- arel = scope . arel ( alias_tracker . aliases )
56
- nodes = arel . constraints . first
56
+ arel = scope . arel ( alias_tracker . aliases )
57
+ nodes = arel . constraints . first
57
58
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
61
63
end
62
- end
63
64
64
- joins << join_type . new ( table , Arel ::Nodes ::On . new ( nodes ) )
65
+ joins << join_type . new ( table , Arel ::Nodes ::On . new ( nodes ) )
65
66
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
70
71
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
73
75
end
74
76
75
77
joins
@@ -88,10 +90,10 @@ def strict_loading?
88
90
end
89
91
90
92
private
91
- def append_constraints ( join , constraints )
93
+ def append_constraints ( connection , join , constraints )
92
94
if join . is_a? ( Arel ::Nodes ::StringJoin )
93
95
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 ) )
95
97
else
96
98
right = join . right
97
99
right . expr = Arel ::Nodes ::And . new ( constraints . unshift right . expr )
0 commit comments