Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
c0a1dc25
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 搜索 >>
提交
c0a1dc25
编写于
8月 14, 2017
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Merge pull request #30169 from awortham/awortham/awortham/fix-sql-distinct-bug
Ensure sum honors distinct on has_many through
上级
5184a56c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
35 addition
and
0 deletion
+35
-0
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+6
-0
activerecord/lib/active_record/relation/calculations.rb
activerecord/lib/active_record/relation/calculations.rb
+3
-0
activerecord/test/cases/associations/has_many_through_associations_test.rb
.../cases/associations/has_many_through_associations_test.rb
+26
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
c0a1dc25
*
Ensure
`sum`
honors
`distinct`
on
`has_many :through`
associations
Fixes #16791
*Aaron Wortham
*
Fix
`COUNT(DISTINCT ...)`
with
`ORDER BY`
and
`LIMIT`
to keep the existing select list.
*Ryuta Kamizono*
...
...
activerecord/lib/active_record/relation/calculations.rb
浏览文件 @
c0a1dc25
...
...
@@ -239,6 +239,9 @@ def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
column
=
aggregate_column
(
column_name
)
select_value
=
operation_over_aggregate_column
(
column
,
operation
,
distinct
)
if
operation
==
"sum"
&&
distinct
select_value
.
distinct
=
true
end
column_alias
=
select_value
.
alias
column_alias
||=
@klass
.
connection
.
column_name_for_operation
(
operation
,
select_value
)
...
...
activerecord/test/cases/associations/has_many_through_associations_test.rb
浏览文件 @
c0a1dc25
...
...
@@ -1132,6 +1132,32 @@ def test_has_many_through_obeys_order_on_through_association
assert_equal
[
"parrot"
,
"bulbul"
],
owner
.
toys
.
map
{
|
r
|
r
.
pet
.
name
}
end
def
test_has_many_through_associations_sum_on_columns
post1
=
Post
.
create
(
title:
"active"
,
body:
"sample"
)
post2
=
Post
.
create
(
title:
"inactive"
,
body:
"sample"
)
person1
=
Person
.
create
(
first_name:
"aaron"
,
followers_count:
1
)
person2
=
Person
.
create
(
first_name:
"schmit"
,
followers_count:
2
)
person3
=
Person
.
create
(
first_name:
"bill"
,
followers_count:
3
)
person4
=
Person
.
create
(
first_name:
"cal"
,
followers_count:
4
)
Reader
.
create
(
post_id:
post1
.
id
,
person_id:
person1
.
id
)
Reader
.
create
(
post_id:
post1
.
id
,
person_id:
person2
.
id
)
Reader
.
create
(
post_id:
post1
.
id
,
person_id:
person3
.
id
)
Reader
.
create
(
post_id:
post1
.
id
,
person_id:
person4
.
id
)
Reader
.
create
(
post_id:
post2
.
id
,
person_id:
person1
.
id
)
Reader
.
create
(
post_id:
post2
.
id
,
person_id:
person2
.
id
)
Reader
.
create
(
post_id:
post2
.
id
,
person_id:
person3
.
id
)
Reader
.
create
(
post_id:
post2
.
id
,
person_id:
person4
.
id
)
active_persons
=
Person
.
joins
(
:readers
).
joins
(
:posts
).
distinct
(
true
).
where
(
"posts.title"
=>
"active"
)
assert_equal
active_persons
.
map
(
&
:followers_count
).
reduce
(:
+
),
10
assert_equal
active_persons
.
sum
(
:followers_count
),
10
assert_equal
active_persons
.
sum
(
:followers_count
),
active_persons
.
map
(
&
:followers_count
).
reduce
(:
+
)
end
def
test_has_many_through_associations_on_new_records_use_null_relations
person
=
Person
.
new
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录