Skip to content

Commit

Permalink
flatten merged join_values before building the joins
Browse files Browse the repository at this point in the history
fixes #10669

While joining_values special treatment is given to string values.
By flattening the array it ensures that string values are detected
as strings and not arrays.
  • Loading branch information
Neeraj Singh authored and amatsuda committed Jul 9, 2013
1 parent 672c297 commit 57e3e0c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
10 changes: 10 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,15 @@
## unreleased ##

* Flatten merged join_values before building the joins.

While joining_values special treatment is given to string values.
By flattening the array it ensures that string values are detected
as strings and not arrays.

Fixes #10669.

*Neeraj Singh and iwiznia*

* Remove extra select and update queries on save/touch/destroy ActiveRecord model
with belongs to reflection with option `touch: true`.

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -799,7 +799,7 @@ def arel
def build_arel
arel = Arel::SelectManager.new(table.engine, table)

build_joins(arel, joins_values) unless joins_values.empty?
build_joins(arel, joins_values.flatten) unless joins_values.empty?

collapse_wheres(arel, (where_values - ['']).uniq)

Expand Down
9 changes: 8 additions & 1 deletion activerecord/test/cases/relation_test.rb
Expand Up @@ -179,7 +179,7 @@ def test_references_values_dont_duplicate
assert_equal ['foo = bar'], relation.where_values
end

def test_relation_merging_with_merged_joins
def test_relation_merging_with_merged_joins_as_symbols
special_comments_with_ratings = SpecialComment.joins(:ratings)
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
Expand All @@ -193,6 +193,13 @@ def test_respond_to_for_non_selected_element
assert_equal false, post.respond_to?(:title), "post should not respond_to?(:body) since invoking it raises exception"
end

def test_relation_merging_with_merged_joins_as_strings
join_string = "LEFT OUTER JOIN #{Rating.quoted_table_name} ON #{SpecialComment.quoted_table_name}.id = #{Rating.quoted_table_name}.comment_id"
special_comments_with_ratings = SpecialComment.joins join_string
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

class RelationMutationTest < ActiveSupport::TestCase
Expand Down

0 comments on commit 57e3e0c

Please sign in to comment.