aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/nested_has_many_through_associations_test.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2010-10-19 00:27:40 +0100
committerJon Leighton <j@jonathanleighton.com>2010-10-19 00:27:40 +0100
commit9ec07348749675110843c44f680da79223218db2 (patch)
treee4a356d2b6ee95232097fac21df5a04118dcc014 /activerecord/test/cases/associations/nested_has_many_through_associations_test.rb
parent78b8c51cb3b0c629152f3bbaf6d8bcf988cc936e (diff)
downloadrails-9ec07348749675110843c44f680da79223218db2.tar.gz
rails-9ec07348749675110843c44f680da79223218db2.tar.bz2
rails-9ec07348749675110843c44f680da79223218db2.zip
Properly support conditions on any of the reflections involved in a nested through association
Diffstat (limited to 'activerecord/test/cases/associations/nested_has_many_through_associations_test.rb')
-rw-r--r--activerecord/test/cases/associations/nested_has_many_through_associations_test.rb40
1 files changed, 37 insertions, 3 deletions
diff --git a/activerecord/test/cases/associations/nested_has_many_through_associations_test.rb b/activerecord/test/cases/associations/nested_has_many_through_associations_test.rb
index 03ec4281d8..c39ec5d139 100644
--- a/activerecord/test/cases/associations/nested_has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/nested_has_many_through_associations_test.rb
@@ -92,8 +92,6 @@ class NestedHasManyThroughAssociationsTest < ActiveRecord::TestCase
assert_no_queries do
assert_equal [luke, david, david], authors.first.subscribers.sort_by(&:nick)
end
-
- # TODO: Add eager loading test using LEFT OUTER JOIN
end
# has_many through
@@ -325,7 +323,7 @@ class NestedHasManyThroughAssociationsTest < ActiveRecord::TestCase
def test_nested_has_many_through_with_a_table_referenced_multiple_times
author = authors(:bob)
- assert_equal [posts(:misc_by_bob), posts(:misc_by_mary)], author.similar_posts.sort_by(&:id)
+ assert_equal [posts(:misc_by_bob), posts(:misc_by_mary), posts(:other_by_bob), posts(:other_by_mary)], author.similar_posts.sort_by(&:id)
# Mary and Bob both have posts in misc, but they are the only ones.
authors = Author.joins(:similar_posts).where('posts.id' => posts(:misc_by_bob).id)
@@ -406,6 +404,42 @@ class NestedHasManyThroughAssociationsTest < ActiveRecord::TestCase
end
end
+ def test_nested_has_many_through_with_conditions_on_through_associations
+ blue, bob = tags(:blue), authors(:bob)
+
+ assert_equal [blue], bob.misc_post_first_blue_tags
+
+ # Pointless condition to force single-query loading
+ assert_includes_and_joins_equal(
+ Author.where('tags.id = tags.id'),
+ [bob], :misc_post_first_blue_tags
+ )
+
+ assert Author.where('tags.id' => 100).joins(:misc_post_first_blue_tags).empty?
+
+ authors = assert_queries(3) { Author.includes(:misc_post_first_blue_tags).to_a }
+ assert_no_queries do
+ assert_equal [blue], authors[2].misc_post_first_blue_tags
+ end
+ end
+
+ def test_nested_has_many_through_with_conditions_on_source_associations
+ blue, bob = tags(:blue), authors(:bob)
+
+ assert_equal [blue], bob.misc_post_first_blue_tags_2
+
+ # Pointless condition to force single-query loading
+ assert_includes_and_joins_equal(
+ Author.where('tags.id = tags.id'),
+ [bob], :misc_post_first_blue_tags_2
+ )
+
+ authors = assert_queries(4) { Author.includes(:misc_post_first_blue_tags_2).to_a }
+ assert_no_queries do
+ assert_equal [blue], authors[2].misc_post_first_blue_tags_2
+ end
+ end
+
private
def assert_includes_and_joins_equal(query, expected, association)