Permalink
Browse files

Merge pull request #14711 from swoker/activerecord_fix_aggregate_meth…

…ods_with_select

Activerecord fix aggregate methods with select
  • Loading branch information...
2 parents 7bb8fd2 + 70fffac commit d447eef20b674b3fb5da4429af6ee23233ef3d8a @rafaelfranca rafaelfranca committed Apr 11, 2014
@@ -289,4 +289,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.
@@ -8,7 +8,15 @@ def initialize(klass, table, association)
def proxy_association
@association
end
+
+ def size
+ @association.size
+ end
+ def empty?
+ @association.empty?
+ end
+
private
def exec_queries
@@ -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
@@ -1197,7 +1197,15 @@ def test_deep_including_through_habtm
author = Author.includes(:posts).references(:posts).reorder(:name).find_by('posts.title IS NOT NULL')
assert_equal authors(:bob), author
end
+
+ test "preloading with a polymorphic association and using the existential predicate but also using a select" do
+ assert_equal authors(:david), authors(:david).essays.includes(:writer).first.writer
+ assert_nothing_raised do
+ authors(:david).essays.includes(:writer).select(:name).any?
+ end
+ end
+
test "preloading with a polymorphic association and using the existential predicate" do
assert_equal authors(:david), authors(:david).essays.includes(:writer).first.writer
@@ -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 d447eef

Please sign in to comment.