Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
f32e3aff
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 搜索 >>
提交
f32e3aff
编写于
1月 19, 2010
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'master' of
git://github.com/rails/rails
上级
c1848f97
9c7c7104
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
38 addition
and
44 deletion
+38
-44
activerecord/lib/active_record/calculations.rb
activerecord/lib/active_record/calculations.rb
+1
-35
activerecord/lib/active_record/named_scope.rb
activerecord/lib/active_record/named_scope.rb
+1
-1
activerecord/lib/active_record/relation/calculation_methods.rb
...erecord/lib/active_record/relation/calculation_methods.rb
+36
-8
未找到文件。
activerecord/lib/active_record/calculations.rb
浏览文件 @
f32e3aff
...
...
@@ -64,41 +64,7 @@ def count(*args)
0
end
# Calculates the average value on a given column. The value is returned as
# a float, or +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
)
end
# Calculates the minimum value on a given column. The value is returned
# with the same data type of the column, or +nil+ if there's no row. See
# +calculate+ for examples with options.
#
# Person.minimum('age') # => 7
def
minimum
(
column_name
,
options
=
{})
calculate
(
:minimum
,
column_name
,
options
)
end
# Calculates the maximum value on a given column. The value is returned
# with the same data type of the column, or +nil+ if there's no row. See
# +calculate+ for examples with options.
#
# Person.maximum('age') # => 93
def
maximum
(
column_name
,
options
=
{})
calculate
(
:maximum
,
column_name
,
options
)
end
# Calculates the sum of values on a given column. The value is returned
# with the same data type of the column, 0 if there's no row. See
# +calculate+ for examples with options.
#
# Person.sum('age') # => 4562
def
sum
(
column_name
,
options
=
{})
calculate
(
:sum
,
column_name
,
options
)
end
delegate
:average
,
:minimum
,
:maximum
,
:sum
,
:to
=>
:scoped
# This calculates aggregate values in the given column. Methods for count, sum, average, minimum, and maximum have been added as shortcuts.
# Options such as <tt>:conditions</tt>, <tt>:order</tt>, <tt>:group</tt>, <tt>:having</tt>, and <tt>:joins</tt> can be passed to customize the query.
...
...
activerecord/lib/active_record/named_scope.rb
浏览文件 @
f32e3aff
...
...
@@ -182,7 +182,7 @@ def count(*args)
end
def
==
(
other
)
to_a
==
other
.
to_a
other
.
respond_to?
(
:to_a
)
?
to_a
==
other
.
to_a
:
false
end
private
...
...
activerecord/lib/active_record/relation/calculation_methods.rb
浏览文件 @
f32e3aff
...
...
@@ -5,20 +5,40 @@ def count(*args)
calculate
(
:count
,
*
construct_count_options_from_args
(
*
args
))
end
def
average
(
column_name
)
calculate
(
:average
,
column_name
)
# Calculates the average value on a given column. The value is returned as
# a float, or +nil+ if there's no row. See +calculate+ for examples with
# options.
#
# Person.average('age') # => 35.8
def
average
(
column_name
,
options
=
{})
calculation_relation
(
options
).
calculate
(
:average
,
column_name
)
end
def
minimum
(
column_name
)
calculate
(
:minimum
,
column_name
)
# Calculates the minimum value on a given column. The value is returned
# with the same data type of the column, or +nil+ if there's no row. See
# +calculate+ for examples with options.
#
# Person.minimum('age') # => 7
def
minimum
(
column_name
,
options
=
{})
calculation_relation
(
options
).
calculate
(
:minimum
,
column_name
)
end
def
maximum
(
column_name
)
calculate
(
:maximum
,
column_name
)
# Calculates the maximum value on a given column. The value is returned
# with the same data type of the column, or +nil+ if there's no row. See
# +calculate+ for examples with options.
#
# Person.maximum('age') # => 93
def
maximum
(
column_name
,
options
=
{})
calculation_relation
(
options
).
calculate
(
:maximum
,
column_name
)
end
def
sum
(
column_name
)
calculate
(
:sum
,
column_name
)
# Calculates the sum of values on a given column. The value is returned
# with the same data type of the column, 0 if there's no row. See
# +calculate+ for examples with options.
#
# Person.sum('age') # => 4562
def
sum
(
column_name
,
options
=
{})
calculation_relation
(
options
).
calculate
(
:sum
,
column_name
)
end
def
calculate
(
operation
,
column_name
,
options
=
{})
...
...
@@ -49,6 +69,14 @@ def calculate(operation, column_name, options = {})
0
end
def
calculation_relation
(
options
=
{})
if
options
.
present?
apply_finder_options
(
options
.
except
(
:distinct
)).
calculation_relation
else
(
eager_loading?
||
includes_values
.
present?
)
?
construct_relation_for_association_calculations
:
self
end
end
private
def
execute_simple_calculation
(
operation
,
column_name
,
distinct
)
#:nodoc:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录