From 00cd3789f6d53163229669ad82a5d87fcdcb49ba Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Wed, 30 Dec 2009 01:12:38 +0530 Subject: Relation#count should look for projections in chained relations and perform the count on the given column Signed-off-by: Pratik Naik --- activerecord/test/cases/relations_test.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 8c1ce07e75..f0ef3e22c5 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -376,7 +376,7 @@ class RelationTest < ActiveRecord::TestCase relations = [] relations << (Post.order('comments.id DESC') & Post.eager_load(:last_comment) & Post.scoped) relations << (Post.eager_load(:last_comment) & Post.order('comments.id DESC') & Post.scoped) - + relations.each do |posts| post = posts.find { |p| p.id == 1 } assert_equal Post.find(1).last_comment, post.last_comment @@ -418,6 +418,9 @@ class RelationTest < ActiveRecord::TestCase Post.update_all(:comments_count => nil) posts = Post.scoped + assert_equal 0, posts.select('comments_count').where('id is not null').order('id').count + assert_equal 0, posts.where('id is not null').select('comments_count').count + assert_equal 7, posts.select('comments_count').count('id') assert_equal 0, posts.select('comments_count').count assert_equal 0, posts.count(:comments_count) @@ -435,4 +438,11 @@ class RelationTest < ActiveRecord::TestCase assert_no_queries { assert_equal 5, best_posts.size } end + def test_count_complex_chained_relations + posts = Post.select('comments_count').where('id is not null').group("author_id").where("comments_count > 0") + + expected = { 1 => 2 } + assert_equal expected, posts.count + end + end -- cgit v1.2.3