diff options
author | Jon Leighton <j@jonathanleighton.com> | 2013-06-22 03:25:43 -0700 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2013-06-22 03:25:43 -0700 |
commit | 4f46ef36aaef217834f3f96d3689d32f6a6761ea (patch) | |
tree | 6a549374c68cbec52f22048245f39c94e5c5247c | |
parent | 88beaee64eeb29bec4a3477dedf6dbbb08b2e108 (diff) | |
parent | 32420bd4bcf20dc21e925474cbb0ac2b46722b80 (diff) | |
download | rails-4f46ef36aaef217834f3f96d3689d32f6a6761ea.tar.gz rails-4f46ef36aaef217834f3f96d3689d32f6a6761ea.tar.bz2 rails-4f46ef36aaef217834f3f96d3689d32f6a6761ea.zip |
Merge pull request #10796 from neerajdotname/10669
flatten merged join_values before building the joins
-rw-r--r-- | activerecord/CHANGELOG.md | 10 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/relation_test.rb | 9 |
3 files changed, 19 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 608b5b64cb..4055dcd484 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,13 @@ +* 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* + * Do not load all child records for inverse case. currently `post.comments.find(Comment.first.id)` would load all diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index ca1de2d4dc..0200fcf69b 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -802,7 +802,7 @@ module ActiveRecord 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) diff --git a/activerecord/test/cases/relation_test.rb b/activerecord/test/cases/relation_test.rb index 55fd068d37..693b36f35c 100644 --- a/activerecord/test/cases/relation_test.rb +++ b/activerecord/test/cases/relation_test.rb @@ -176,7 +176,7 @@ module ActiveRecord 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 @@ -190,6 +190,13 @@ module ActiveRecord 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 |