mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix relation merger issue with left_outer_joins
This commit is contained in:
parent
562dd0494a
commit
899a801413
4 changed files with 37 additions and 1 deletions
|
@ -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.
|
* Don't allow destroyed object mutation after `save` or `save!` is called.
|
||||||
|
|
||||||
*Ryuta Kamizono*
|
*Ryuta Kamizono*
|
||||||
|
|
|
@ -52,7 +52,7 @@ module ActiveRecord
|
||||||
|
|
||||||
NORMAL_VALUES = Relation::VALUE_METHODS -
|
NORMAL_VALUES = Relation::VALUE_METHODS -
|
||||||
Relation::CLAUSE_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
|
def normal_values
|
||||||
NORMAL_VALUES
|
NORMAL_VALUES
|
||||||
|
@ -79,6 +79,7 @@ module ActiveRecord
|
||||||
merge_clauses
|
merge_clauses
|
||||||
merge_preloads
|
merge_preloads
|
||||||
merge_joins
|
merge_joins
|
||||||
|
merge_outer_joins
|
||||||
|
|
||||||
relation
|
relation
|
||||||
end
|
end
|
||||||
|
@ -129,6 +130,29 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
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
|
def merge_multi_values
|
||||||
if other.reordering_value
|
if other.reordering_value
|
||||||
# override any order specified in the original relation
|
# override any order specified in the original relation
|
||||||
|
|
|
@ -979,6 +979,8 @@ module ActiveRecord
|
||||||
case join
|
case join
|
||||||
when Hash, Symbol, Array
|
when Hash, Symbol, Array
|
||||||
:association_join
|
:association_join
|
||||||
|
when ActiveRecord::Associations::JoinDependency
|
||||||
|
:stashed_join
|
||||||
else
|
else
|
||||||
raise ArgumentError, "only Hash, Symbol and Array are allowed"
|
raise ArgumentError, "only Hash, Symbol and Array are allowed"
|
||||||
end
|
end
|
||||||
|
|
|
@ -72,6 +72,12 @@ class RelationMergingTest < ActiveRecord::TestCase
|
||||||
assert_equal 1, comments.count
|
assert_equal 1, comments.count
|
||||||
end
|
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
|
def test_relation_merging_with_association
|
||||||
assert_queries(2) do # one for loading post, and another one merged query
|
assert_queries(2) do # one for loading post, and another one merged query
|
||||||
post = Post.where(body: "Such a lovely day").first
|
post = Post.where(body: "Such a lovely day").first
|
||||||
|
|
Loading…
Reference in a new issue