aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations
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
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')
-rw-r--r--activerecord/test/cases/associations/cascaded_eager_loading_test.rb6
-rw-r--r--activerecord/test/cases/associations/eager_test.rb4
-rw-r--r--activerecord/test/cases/associations/nested_has_many_through_associations_test.rb40
3 files changed, 42 insertions, 8 deletions
diff --git a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb
index 0e9c8a2639..5b24d49a7d 100644
--- a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb
+++ b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb
@@ -15,7 +15,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
authors = Author.find(:all, :include=>{:posts=>:comments}, :order=>"authors.id")
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
- assert_equal 2, authors[1].posts.size
+ assert_equal 3, authors[1].posts.size
assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
end
@@ -23,7 +23,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
authors = Author.find(:all, :include=>[{:posts=>:comments}, :categorizations], :order=>"authors.id")
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
- assert_equal 2, authors[1].posts.size
+ assert_equal 3, authors[1].posts.size
assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
assert_equal 1, authors[0].categorizations.size
assert_equal 2, authors[1].categorizations.size
@@ -56,7 +56,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
authors = Author.find(:all, :include=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id")
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
- assert_equal 2, authors[1].posts.size
+ assert_equal 3, authors[1].posts.size
assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
end
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index 2ff0714e9f..6b910ae2a0 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -53,8 +53,8 @@ class EagerAssociationTest < ActiveRecord::TestCase
def test_with_ordering
list = Post.find(:all, :include => :comments, :order => "posts.id DESC")
- [:misc_by_mary, :misc_by_bob, :eager_other, :sti_habtm, :sti_post_and_comments,
- :sti_comments, :authorless, :thinking, :welcome
+ [:other_by_mary, :other_by_bob, :misc_by_mary, :misc_by_bob, :eager_other,
+ :sti_habtm, :sti_post_and_comments, :sti_comments, :authorless, :thinking, :welcome
].each_with_index do |post, index|
assert_equal posts(post), list[index]
end
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)