未验证 提交 d8e7d6b0 编写于 作者: R Ryuta Kamizono 提交者: GitHub

Merge pull request #27860 from meinac/fix_left_joins_behaviour_with_merge

Fix relation merger issue with `left_outer_joins`.
* 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*
......
......@@ -52,7 +52,7 @@ def initialize(relation, other)
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 @@ def merge
merge_clauses
merge_preloads
merge_joins
merge_outer_joins
relation
end
......@@ -129,6 +130,29 @@ def merge_joins
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
......
......@@ -979,6 +979,8 @@ def build_left_outer_joins(manager, outer_joins, aliases)
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
......
......@@ -72,6 +72,12 @@ def test_relation_merging_with_joins
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册