Skip to content
Browse files

Merge branch 'rm-fix-13648'

Includes #14711 and some cleanup
commits.

Fixes #13648
  • Loading branch information...
2 parents 7bb8fd2 + 8d11fbf commit c8a70660d46343d05c954c8a9b830166aa7335e9 @rafaelfranca rafaelfranca committed Apr 11, 2014
View
7 activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* Fixed error for aggregate methods (`empty?`, `any?`, `count`) with `select`
+ which created invalid SQL.
+
+ Fixes #13648.
+
+ *Simon Woker*
+
* PostgreSQL adapter only warns once for every missing OID per connection.
Fixes #14275.
View
8 activerecord/lib/active_record/association_relation.rb
@@ -9,6 +9,14 @@ def proxy_association
@association
end
+ def size
+ @association.size
+ end
+
+ def empty?
+ @association.empty?
+ end
+
private
def exec_queries
View
5 activerecord/lib/active_record/relation.rb
@@ -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
View
8 activerecord/test/cases/associations/eager_test.rb
@@ -1198,6 +1198,14 @@ def test_deep_including_through_habtm
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
View
10 activerecord/test/cases/relations_test.rb
@@ -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_not posts.empty?
+ end
+
def test_select_takes_a_variable_list_of_args
david = developers(:david)

0 comments on commit c8a7066

Please sign in to comment.
Something went wrong with that request. Please try again.