Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
58772397
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
58772397
编写于
3月 14, 2016
作者:
E
Erik Michaels-Ober
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Forward ActiveRecord::Relation#count to Enumerable#count if block given
上级
dda31d59
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
28 addition
and
6 deletion
+28
-6
activerecord/lib/active_record/associations/collection_association.rb
.../lib/active_record/associations/collection_association.rb
+5
-2
activerecord/lib/active_record/associations/collection_proxy.rb
...record/lib/active_record/associations/collection_proxy.rb
+9
-3
activerecord/lib/active_record/relation/calculations.rb
activerecord/lib/active_record/relation/calculations.rb
+5
-1
activerecord/test/cases/calculations_test.rb
activerecord/test/cases/calculations_test.rb
+4
-0
activerecord/test/cases/relations_test.rb
activerecord/test/cases/relations_test.rb
+5
-0
未找到文件。
activerecord/lib/active_record/associations/collection_association.rb
浏览文件 @
58772397
...
...
@@ -246,9 +246,12 @@ def destroy_all
end
end
# Count all records using SQL. Construct options and pass them with
# scope to the target class's +count+.
# Returns the number of records. If no arguments are given, it counts all
# columns using SQL. If one argument is given, it counts only the passed
# column using SQL. If a block is given, it counts the number of records
# yielding a true value.
def
count
(
column_name
=
nil
)
return
super
if
block_given?
relation
=
scope
if
association_scope
.
distinct_value
# This is needed because 'SELECT count(DISTINCT *)..' is not valid SQL.
...
...
activerecord/lib/active_record/associations/collection_proxy.rb
浏览文件 @
58772397
...
...
@@ -715,12 +715,13 @@ def distinct
end
alias
uniq
distinct
# Count all records
using SQL
.
# Count all records.
#
# class Person < ActiveRecord::Base
# has_many :pets
# end
#
# # This will perform the count using SQL.
# person.pets.count # => 3
# person.pets
# # => [
...
...
@@ -728,8 +729,13 @@ def distinct
# # #<Pet id: 2, name: "Spook", person_id: 1>,
# # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
# # ]
def
count
(
column_name
=
nil
)
@association
.
count
(
column_name
)
#
# Passing a block will select all of a person's pets in SQL and then
# perform the count using Ruby.
#
# person.pets.count { |pet| pet.name.include?('-') } # => 2
def
count
(
column_name
=
nil
,
&
block
)
@association
.
count
(
column_name
,
&
block
)
end
# Returns the size of the collection. If the collection hasn't been loaded,
...
...
activerecord/lib/active_record/relation/calculations.rb
浏览文件 @
58772397
...
...
@@ -37,7 +37,11 @@ module Calculations
# Note: not all valid {Relation#select}[rdoc-ref:QueryMethods#select] expressions are valid #count expressions. The specifics differ
# between databases. In invalid cases, an error from the database is thrown.
def
count
(
column_name
=
nil
)
calculate
(
:count
,
column_name
)
if
block_given?
to_a
.
count
{
|*
block_args
|
yield
(
*
block_args
)
}
else
calculate
(
:count
,
column_name
)
end
end
# Calculates the average value on a given column. Returns +nil+ if there's
...
...
activerecord/test/cases/calculations_test.rb
浏览文件 @
58772397
...
...
@@ -482,6 +482,10 @@ def test_count_with_where_and_order
assert_equal
1
,
Account
.
where
(
firm_name:
'37signals'
).
order
(
:firm_name
).
reverse_order
.
count
end
def
test_count_with_block
assert_equal
4
,
Account
.
count
{
|
account
|
account
.
credit_limit
.
modulo
(
10
).
zero?
}
end
def
test_should_sum_expression
# Oracle adapter returns floating point value 636.0 after SUM
if
current_adapter?
(
:OracleAdapter
)
...
...
activerecord/test/cases/relations_test.rb
浏览文件 @
58772397
...
...
@@ -1086,6 +1086,11 @@ def test_count
assert_equal
9
,
posts
.
where
(
:comments_count
=>
0
).
count
end
def
test_count_with_block
posts
=
Post
.
all
assert_equal
10
,
posts
.
count
{
|
p
|
p
.
comments_count
.
even?
}
end
def
test_count_on_association_relation
author
=
Author
.
last
another_author
=
Author
.
first
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录