1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #27860 from meinac/fix_left_joins_behaviour_with_merge

Fix relation merger issue with `left_outer_joins`.
This commit is contained in:
Ryuta Kamizono 2018-01-16 01:39:14 +09:00 committed by GitHub
commit d8e7d6b03c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 1 deletions

View file

@ -1,3 +1,7 @@
* Fix relation merger issue with `left_outer_joins`.
*Mehmet Emin İNAÇ*
* Don't allow destroyed object mutation after `save` or `save!` is called.
*Ryuta Kamizono*

View file

@ -52,7 +52,7 @@ module ActiveRecord
NORMAL_VALUES = Relation::VALUE_METHODS -
Relation::CLAUSE_METHODS -
[:includes, :preload, :joins, :order, :reverse_order, :lock, :create_with, :reordering] # :nodoc:
[:includes, :preload, :joins, :left_outer_joins, :order, :reverse_order, :lock, :create_with, :reordering] # :nodoc:
def normal_values
NORMAL_VALUES
@ -79,6 +79,7 @@ module ActiveRecord
merge_clauses
merge_preloads
merge_joins
merge_outer_joins
relation
end
@ -129,6 +130,29 @@ module ActiveRecord
end
end
def merge_outer_joins
return if other.left_outer_joins_values.blank?
if other.klass == relation.klass
relation.left_outer_joins!(*other.left_outer_joins_values)
else
alias_tracker = nil
joins_dependency = other.left_outer_joins_values.map do |join|
case join
when Hash, Symbol, Array
alias_tracker ||= other.alias_tracker
ActiveRecord::Associations::JoinDependency.new(
other.klass, other.table, join, alias_tracker
)
else
join
end
end
relation.left_outer_joins!(*joins_dependency)
end
end
def merge_multi_values
if other.reordering_value
# override any order specified in the original relation

View file

@ -979,6 +979,8 @@ module ActiveRecord
case join
when Hash, Symbol, Array
:association_join
when ActiveRecord::Associations::JoinDependency
:stashed_join
else
raise ArgumentError, "only Hash, Symbol and Array are allowed"
end

View file

@ -72,6 +72,12 @@ class RelationMergingTest < ActiveRecord::TestCase
assert_equal 1, comments.count
end
def test_relation_merging_with_left_outer_joins
comments = Comment.joins(:post).where(body: "Thank you for the welcome").merge(Post.left_outer_joins(:author).where(body: "Such a lovely day"))
assert_equal 1, comments.count
end
def test_relation_merging_with_association
assert_queries(2) do # one for loading post, and another one merged query
post = Post.where(body: "Such a lovely day").first