aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
authorWill Bryant <will.bryant@gmail.com>2008-09-27 15:28:21 +1200
committerMichael Koziarski <michael@koziarski.com>2008-10-10 16:58:39 +0200
commit4c05055487e149bfa4152c1b42f3519671ca22ac (patch)
treee48d836fa25a2c06544b792f4d72c0f7051be9ee /activerecord/test/cases
parent28393e6e9c9368036e65e77175ea4f65a862259c (diff)
downloadrails-4c05055487e149bfa4152c1b42f3519671ca22ac.tar.gz
rails-4c05055487e149bfa4152c1b42f3519671ca22ac.tar.bz2
rails-4c05055487e149bfa4152c1b42f3519671ca22ac.zip
explicitly including child associations that are also included in the parent association definition should not result in double records in the collection/double loads (#1110)
Signed-off-by: Michael Koziarski <michael@koziarski.com> [#1110 state:committed]
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r--activerecord/test/cases/associations/eager_test.rb42
1 files changed, 41 insertions, 1 deletions
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index 7f42577ab0..5f43975d5a 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -18,7 +18,7 @@ require 'models/developer'
require 'models/project'
class EagerAssociationTest < ActiveRecord::TestCase
- fixtures :posts, :comments, :authors, :categories, :categories_posts,
+ fixtures :posts, :comments, :authors, :author_addresses, :categories, :categories_posts,
:companies, :accounts, :tags, :taggings, :people, :readers,
:owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books,
:developers, :projects, :developers_projects
@@ -111,6 +111,46 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
end
+ def test_finding_with_includes_on_has_many_association_with_same_include_includes_only_once
+ author_id = authors(:david).id
+ author = assert_queries(3) { Author.find(author_id, :include => {:posts_with_comments => :comments}) } # find the author, then find the posts, then find the comments
+ author.posts_with_comments.each do |post_with_comments|
+ assert_equal post_with_comments.comments.length, post_with_comments.comments.count
+ assert_equal nil, post_with_comments.comments.uniq!
+ end
+ end
+
+ def test_finding_with_includes_on_has_one_assocation_with_same_include_includes_only_once
+ author = authors(:david)
+ post = author.post_about_thinking_with_last_comment
+ last_comment = post.last_comment
+ author = assert_queries(3) { Author.find(author.id, :include => {:post_about_thinking_with_last_comment => :last_comment})} # find the author, then find the posts, then find the comments
+ assert_no_queries do
+ assert_equal post, author.post_about_thinking_with_last_comment
+ assert_equal last_comment, author.post_about_thinking_with_last_comment.last_comment
+ end
+ end
+
+ def test_finding_with_includes_on_belongs_to_association_with_same_include_includes_only_once
+ post = posts(:welcome)
+ author = post.author
+ author_address = author.author_address
+ post = assert_queries(3) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author, then find the address
+ assert_no_queries do
+ assert_equal author, post.author_with_address
+ assert_equal author_address, post.author_with_address.author_address
+ end
+ end
+
+ def test_finding_with_includes_on_null_belongs_to_association_with_same_include_includes_only_once
+ post = posts(:welcome)
+ post.update_attributes!(:author => nil)
+ post = assert_queries(2) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author which is null so no query for the address
+ assert_no_queries do
+ assert_equal nil, post.author_with_address
+ end
+ end
+
def test_loading_from_an_association
posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id")
assert_equal 2, posts.first.comments.size