Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
8635f6a4
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,发现更多精彩内容 >>
提交
8635f6a4
编写于
6月 24, 2012
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #6842 from ernie/handle-non-strings-in-grouped-calculations
Stop assuming strings for grouped calculations
上级
ebe8a454
a1c05dd8
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
22 addition
and
6 deletion
+22
-6
activerecord/lib/active_record/relation/calculations.rb
activerecord/lib/active_record/relation/calculations.rb
+17
-6
activerecord/test/cases/calculations_test.rb
activerecord/test/cases/calculations_test.rb
+5
-0
未找到文件。
activerecord/lib/active_record/relation/calculations.rb
浏览文件 @
8635f6a4
...
...
@@ -262,10 +262,16 @@ def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
end
def
execute_grouped_calculation
(
operation
,
column_name
,
distinct
)
#:nodoc:
group_attr
=
group_values
association
=
@klass
.
reflect_on_association
(
group_attr
.
first
.
to_sym
)
associated
=
group_attr
.
size
==
1
&&
association
&&
association
.
macro
==
:belongs_to
# only count belongs_to associations
group_fields
=
Array
(
associated
?
association
.
foreign_key
:
group_attr
)
group_attrs
=
group_values
if
group_attrs
.
first
.
respond_to?
(
:to_sym
)
association
=
@klass
.
reflect_on_association
(
group_attrs
.
first
.
to_sym
)
associated
=
group_attrs
.
size
==
1
&&
association
&&
association
.
macro
==
:belongs_to
# only count belongs_to associations
group_fields
=
Array
(
associated
?
association
.
foreign_key
:
group_attrs
)
else
group_fields
=
group_attrs
end
group_aliases
=
group_fields
.
map
{
|
field
|
column_alias_for
(
field
)
}
group_columns
=
group_aliases
.
zip
(
group_fields
).
map
{
|
aliaz
,
field
|
[
aliaz
,
column_for
(
field
)]
...
...
@@ -288,10 +294,14 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
select_values
+=
select_values
unless
having_values
.
empty?
select_values
.
concat
group_fields
.
zip
(
group_aliases
).
map
{
|
field
,
aliaz
|
"
#{
field
}
AS
#{
aliaz
}
"
if
field
.
respond_to?
(
:as
)
field
.
as
(
aliaz
)
else
"
#{
field
}
AS
#{
aliaz
}
"
end
}
relation
=
except
(
:group
).
group
(
group
.
join
(
','
)
)
relation
=
except
(
:group
).
group
(
group
)
relation
.
select_values
=
select_values
calculated_data
=
@klass
.
connection
.
select_all
(
relation
,
nil
,
bind_values
)
...
...
@@ -321,6 +331,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
# column_alias_for("count(*)") # => "count_all"
# column_alias_for("count", "id") # => "count_id"
def
column_alias_for
(
*
keys
)
keys
.
map!
{
|
k
|
k
.
respond_to?
(
:to_sql
)
?
k
.
to_sql
:
k
}
table_name
=
keys
.
join
(
' '
)
table_name
.
downcase!
table_name
.
gsub!
(
/\*/
,
'all'
)
...
...
activerecord/test/cases/calculations_test.rb
浏览文件 @
8635f6a4
...
...
@@ -61,6 +61,11 @@ def test_should_group_by_field
[
1
,
6
,
2
].
each
{
|
firm_id
|
assert
c
.
keys
.
include?
(
firm_id
)
}
end
def
test_should_group_by_arel_attribute
c
=
Account
.
sum
(
:credit_limit
,
:group
=>
Account
.
arel_table
[
:firm_id
])
[
1
,
6
,
2
].
each
{
|
firm_id
|
assert
c
.
keys
.
include?
(
firm_id
)
}
end
def
test_should_group_by_multiple_fields
c
=
Account
.
group
(
'firm_id'
,
:credit_limit
).
count
(
:all
)
[
[
nil
,
50
],
[
1
,
50
],
[
6
,
50
],
[
6
,
55
],
[
9
,
53
],
[
2
,
60
]
].
each
{
|
firm_and_limit
|
assert
c
.
keys
.
include?
(
firm_and_limit
)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录