aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/merger.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-04-05 06:09:44 +0900
committerRyuta Kamizono <kamipo@gmail.com>2019-04-05 06:40:53 +0900
commit8f05035b7e595e2086759ee10ec9df9431e5e351 (patch)
tree02b91b4c73aec9e20e1325c5e54667729485cbe1 /activerecord/lib/active_record/relation/merger.rb
parent50fba828d533ff75671ca8b83337dd99aa613ff7 (diff)
downloadrails-8f05035b7e595e2086759ee10ec9df9431e5e351.tar.gz
rails-8f05035b7e595e2086759ee10ec9df9431e5e351.tar.bz2
rails-8f05035b7e595e2086759ee10ec9df9431e5e351.zip
Stash `left_joins` into `joins` to deduplicate redundant LEFT JOIN
Originally the `JoinDependency` has the deduplication for eager loading (LEFT JOIN). This re-uses that deduplication for `left_joins`. And also, This makes left join order into part of joins, i.e.: Before: ``` association joins -> stash joins (eager loading, etc) -> string joins -> left joins ``` After: ``` association joins -> stash joins (eager loading, left joins, etc) -> string joins ``` Now string joins are able to refer left joins. Fixes #34325. Fixes #34332. Fixes #34536.
Diffstat (limited to 'activerecord/lib/active_record/relation/merger.rb')
-rw-r--r--activerecord/lib/active_record/relation/merger.rb23
1 files changed, 7 insertions, 16 deletions
diff --git a/activerecord/lib/active_record/relation/merger.rb b/activerecord/lib/active_record/relation/merger.rb
index 4de7465128..6bb77b355c 100644
--- a/activerecord/lib/active_record/relation/merger.rb
+++ b/activerecord/lib/active_record/relation/merger.rb
@@ -117,16 +117,14 @@ module ActiveRecord
if other.klass == relation.klass
relation.joins!(*other.joins_values)
else
- joins_dependency = other.joins_values.map do |join|
+ associations, others = other.joins_values.partition do |join|
case join
- when Hash, Symbol, Array
- other.send(:construct_join_dependency, join)
- else
- join
+ when Hash, Symbol, Array; true
end
end
- relation.joins!(*joins_dependency)
+ join_dependency = other.construct_join_dependency(associations)
+ relation.joins!(join_dependency, *others)
end
end
@@ -136,16 +134,9 @@ module ActiveRecord
if other.klass == relation.klass
relation.left_outer_joins!(*other.left_outer_joins_values)
else
- joins_dependency = other.left_outer_joins_values.map do |join|
- case join
- when Hash, Symbol, Array
- other.send(:construct_join_dependency, join)
- else
- join
- end
- end
-
- relation.left_outer_joins!(*joins_dependency)
+ associations = other.left_outer_joins_values
+ join_dependency = other.construct_join_dependency(associations)
+ relation.joins!(join_dependency)
end
end