Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
dcff027a
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,发现更多精彩内容 >>
提交
dcff027a
编写于
10月 13, 2013
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #12518 from vipulnsward/remove_count_options
`Relation#count` doesn't use options anymore.
上级
3a209398
42d3d3c2
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
17 addition
and
20 deletion
+17
-20
activerecord/lib/active_record/associations/collection_association.rb
.../lib/active_record/associations/collection_association.rb
+1
-3
activerecord/lib/active_record/associations/collection_proxy.rb
...record/lib/active_record/associations/collection_proxy.rb
+2
-2
activerecord/lib/active_record/null_relation.rb
activerecord/lib/active_record/null_relation.rb
+1
-1
activerecord/lib/active_record/relation/calculations.rb
activerecord/lib/active_record/relation/calculations.rb
+12
-13
activerecord/test/cases/relations_test.rb
activerecord/test/cases/relations_test.rb
+1
-1
未找到文件。
activerecord/lib/active_record/associations/collection_association.rb
浏览文件 @
dcff027a
...
...
@@ -195,9 +195,7 @@ def destroy_all
# Count all records using SQL. Construct options and pass them with
# scope to the target class's +count+.
def
count
(
column_name
=
nil
,
count_options
=
{})
column_name
,
count_options
=
nil
,
column_name
if
column_name
.
is_a?
(
Hash
)
def
count
(
column_name
=
nil
)
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
浏览文件 @
dcff027a
...
...
@@ -669,8 +669,8 @@ def distinct
# # #<Pet id: 2, name: "Spook", person_id: 1>,
# # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
# # ]
def
count
(
column_name
=
nil
,
options
=
{}
)
@association
.
count
(
column_name
,
options
)
def
count
(
column_name
=
nil
)
@association
.
count
(
column_name
)
end
# Returns the size of the collection. If the collection hasn't been loaded,
...
...
activerecord/lib/active_record/null_relation.rb
浏览文件 @
dcff027a
...
...
@@ -50,7 +50,7 @@ def sum(*)
0
end
def
calculate
(
_operation
,
_column_name
,
_options
=
{}
)
def
calculate
(
_operation
,
_column_name
)
if
_operation
==
:count
0
else
...
...
activerecord/lib/active_record/relation/calculations.rb
浏览文件 @
dcff027a
...
...
@@ -19,17 +19,16 @@ module Calculations
#
# Person.group(:city).count
# # => { 'Rome' => 5, 'Paris' => 3 }
def
count
(
column_name
=
nil
,
options
=
{})
column_name
,
options
=
nil
,
column_name
if
column_name
.
is_a?
(
Hash
)
calculate
(
:count
,
column_name
,
options
)
def
count
(
column_name
=
nil
)
calculate
(
:count
,
column_name
)
end
# Calculates the average value on a given column. Returns +nil+ if there's
# no row. See +calculate+ for examples with options.
#
# Person.average(:age) # => 35.8
def
average
(
column_name
,
options
=
{}
)
calculate
(
:average
,
column_name
,
options
)
def
average
(
column_name
)
calculate
(
:average
,
column_name
)
end
# Calculates the minimum value on a given column. The value is returned
...
...
@@ -37,8 +36,8 @@ def average(column_name, options = {})
# +calculate+ for examples with options.
#
# Person.minimum(:age) # => 7
def
minimum
(
column_name
,
options
=
{}
)
calculate
(
:minimum
,
column_name
,
options
)
def
minimum
(
column_name
)
calculate
(
:minimum
,
column_name
)
end
# Calculates the maximum value on a given column. The value is returned
...
...
@@ -46,8 +45,8 @@ def minimum(column_name, options = {})
# +calculate+ for examples with options.
#
# Person.maximum(:age) # => 93
def
maximum
(
column_name
,
options
=
{}
)
calculate
(
:maximum
,
column_name
,
options
)
def
maximum
(
column_name
)
calculate
(
:maximum
,
column_name
)
end
# Calculates the sum of values on a given column. The value is returned
...
...
@@ -90,15 +89,15 @@ def sum(*args)
# Person.group(:last_name).having("min(age) > 17").minimum(:age)
#
# Person.sum("2 * age")
def
calculate
(
operation
,
column_name
,
options
=
{}
)
def
calculate
(
operation
,
column_name
)
if
column_name
.
is_a?
(
Symbol
)
&&
attribute_alias?
(
column_name
)
column_name
=
attribute_alias
(
column_name
)
end
if
has_include?
(
column_name
)
construct_relation_for_association_calculations
.
calculate
(
operation
,
column_name
,
options
)
construct_relation_for_association_calculations
.
calculate
(
operation
,
column_name
)
else
perform_calculation
(
operation
,
column_name
,
options
)
perform_calculation
(
operation
,
column_name
)
end
end
...
...
@@ -181,7 +180,7 @@ def has_include?(column_name)
eager_loading?
||
(
includes_values
.
present?
&&
(
column_name
||
references_eager_loaded_tables?
))
end
def
perform_calculation
(
operation
,
column_name
,
options
=
{}
)
def
perform_calculation
(
operation
,
column_name
)
operation
=
operation
.
to_s
.
downcase
# If #count is used with #distinct / #uniq it is considered distinct. (eg. relation.distinct.count)
...
...
activerecord/test/cases/relations_test.rb
浏览文件 @
dcff027a
...
...
@@ -295,7 +295,7 @@ def test_null_relation_content_size_methods
def
test_null_relation_calculations_methods
assert_no_queries
do
assert_equal
0
,
Developer
.
none
.
count
assert_equal
0
,
Developer
.
none
.
calculate
(
:count
,
nil
,
{}
)
assert_equal
0
,
Developer
.
none
.
calculate
(
:count
,
nil
)
assert_equal
nil
,
Developer
.
none
.
calculate
(
:average
,
'salary'
)
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录