mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #14711 from swoker/activerecord_fix_aggregate_methods_with_select
Activerecord fix aggregate methods with select
This commit is contained in:
commit
d447eef20b
5 changed files with 35 additions and 3 deletions
|
@ -289,4 +289,11 @@
|
||||||
|
|
||||||
*Yves Senn*
|
*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.
|
Please check [4-1-stable](https://github.com/rails/rails/blob/4-1-stable/activerecord/CHANGELOG.md) for previous changes.
|
||||||
|
|
|
@ -9,6 +9,14 @@ module ActiveRecord
|
||||||
@association
|
@association
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def size
|
||||||
|
@association.size
|
||||||
|
end
|
||||||
|
|
||||||
|
def empty?
|
||||||
|
@association.empty?
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def exec_queries
|
def exec_queries
|
||||||
|
|
|
@ -238,7 +238,7 @@ module ActiveRecord
|
||||||
|
|
||||||
# Returns size of the records.
|
# Returns size of the records.
|
||||||
def size
|
def size
|
||||||
loaded? ? @records.length : count
|
loaded? ? @records.length : count(:all)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns true if there are no records.
|
# Returns true if there are no records.
|
||||||
|
@ -248,8 +248,7 @@ module ActiveRecord
|
||||||
if limit_value == 0
|
if limit_value == 0
|
||||||
true
|
true
|
||||||
else
|
else
|
||||||
# FIXME: This count is not compatible with #select('authors.*') or other select narrows
|
c = count(:all)
|
||||||
c = count
|
|
||||||
c.respond_to?(:zero?) ? c.zero? : c.empty?
|
c.respond_to?(:zero?) ? c.zero? : c.empty?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1198,6 +1198,14 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
||||||
assert_equal authors(:bob), author
|
assert_equal authors(:bob), author
|
||||||
end
|
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
|
test "preloading with a polymorphic association and using the existential predicate" do
|
||||||
assert_equal authors(:david), authors(:david).essays.includes(:writer).first.writer
|
assert_equal authors(:david), authors(:david).essays.includes(:writer).first.writer
|
||||||
|
|
||||||
|
|
|
@ -824,6 +824,16 @@ class RelationTest < ActiveRecord::TestCase
|
||||||
assert_raises(ActiveRecord::ActiveRecordError) { Author.limit(10).delete_all }
|
assert_raises(ActiveRecord::ActiveRecordError) { Author.limit(10).delete_all }
|
||||||
end
|
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
|
def test_select_takes_a_variable_list_of_args
|
||||||
david = developers(:david)
|
david = developers(:david)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue