Skip to content

Commit

Permalink
Merge branch 'rm-fix-13648'
Browse files Browse the repository at this point in the history
Includes #14711 and some cleanup
commits.

Fixes #13648
  • Loading branch information
rafaelfranca committed Apr 11, 2014
2 parents 7bb8fd2 + 8d11fbf commit c8a7066
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
7 changes: 7 additions & 0 deletions 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.
Expand Down
8 changes: 8 additions & 0 deletions activerecord/lib/active_record/association_relation.rb
Expand Up @@ -9,6 +9,14 @@ def proxy_association
@association
end

def size
@association.size
end

def empty?
@association.empty?
end

private

def exec_queries
Expand Down
5 changes: 2 additions & 3 deletions activerecord/lib/active_record/relation.rb
Expand Up @@ -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.
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/associations/eager_test.rb
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -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)

Expand Down

0 comments on commit c8a7066

Please sign in to comment.