Permalink
Browse files

Fix error for aggregate methods with select, see issue #13648

  • Loading branch information...
1 parent d8d0fcb commit afd4d8205e6a3264c30a29e4a2de0f1e71ef0717 @swoker swoker committed Apr 10, 2014
@@ -264,4 +264,11 @@
*Yves Senn*
+* Fixed error for aggregate methods (empty?, any?, count) with select()
+ which created invalid SQL
+
+ Fixes #13648
+
+ *Simon Woker*
+
Please check [4-1-stable](https://github.com/rails/rails/blob/4-1-stable/activerecord/CHANGELOG.md) for previous changes.
@@ -238,7 +238,7 @@ def as_json(options = nil) #:nodoc:
# Returns size of the records.
def size
- loaded? ? @records.length : count
+ loaded? ? @records.length : count(:all)
end
# Returns true if there are no records.
@@ -248,8 +248,7 @@ def empty?
if limit_value == 0
true
else
- # FIXME: This count is not compatible with #select('authors.*') or other select narrows
- c = count
+ c = count(:all)
c.respond_to?(:zero?) ? c.zero? : c.empty?
end
end
@@ -824,6 +824,16 @@ def test_delete_all_limit_error
assert_raises(ActiveRecord::ActiveRecordError) { Author.limit(10).delete_all }
end
+ def test_select_with_aggregates
+ posts = Post.select(:title, :body)
+
+ assert_equal 11, posts.count(:all)
+ assert_equal 11, posts.size
+ assert posts.any?
+ assert posts.many?
+ assert ! posts.empty?
+ end
+
def test_select_takes_a_variable_list_of_args
david = developers(:david)

0 comments on commit afd4d82

Please sign in to comment.