Skip to content

Commit

Permalink
Make outer joins on proper parent
Browse files Browse the repository at this point in the history
Outer joins were being built on the root relation klass rather than the
one specified in the join dependency root
  • Loading branch information
huoxito committed Dec 31, 2013
1 parent 969a077 commit 8e1f26c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def join_constraints(outer_joins)
walk join_root, oj.join_root
else
oj.join_root.children.flat_map { |child|
make_outer_joins join_root, child
make_outer_joins oj.join_root, child
}
end
}
Expand Down
12 changes: 11 additions & 1 deletion activerecord/test/cases/relation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ def test_relation_merging_with_merged_joins_as_symbols
assert_equal 3, authors(:david).posts.merge(posts_with_special_comments_with_ratings).count.length
end

def test_relation_merging_with_joins_as_join_dependency_pick_proper_parent
post = Post.create!(title: "haha", body: "huhu")
comment = post.comments.create!(body: "hu")
3.times { comment.ratings.create! }

relation = Post.joins Associations::JoinDependency.new(Post, :comments, [])
relation = relation.joins Associations::JoinDependency.new(Comment, :ratings, [])

assert_equal 3, relation.pluck(:id).select { |id| id == post.id }.count
end

def test_respond_to_for_non_selected_element
post = Post.select(:title).first
assert_equal false, post.respond_to?(:body), "post should not respond_to?(:body) since invoking it raises exception"
Expand All @@ -221,6 +232,5 @@ def test_relation_merging_with_merged_joins_as_strings
posts_with_special_comments_with_ratings = Post.group("posts.id").joins(:special_comments).merge(special_comments_with_ratings)
assert_equal 3, authors(:david).posts.merge(posts_with_special_comments_with_ratings).count.length
end

end
end

0 comments on commit 8e1f26c

Please sign in to comment.