Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
a7e715e1
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,发现更多精彩内容 >>
提交
a7e715e1
编写于
6月 19, 2012
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'aderyabin-fix7'
Closes #6007
上级
2f79d91e
1579f128
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
67 addition
and
23 deletion
+67
-23
activerecord/lib/active_record/associations/collection_association.rb
.../lib/active_record/associations/collection_association.rb
+1
-12
activerecord/lib/active_record/relation/calculations.rb
activerecord/lib/active_record/relation/calculations.rb
+20
-11
activerecord/test/cases/calculations_test.rb
activerecord/test/cases/calculations_test.rb
+46
-0
未找到文件。
activerecord/lib/active_record/associations/collection_association.rb
浏览文件 @
a7e715e1
...
...
@@ -50,18 +50,7 @@ def ids_reader
end
else
column
=
"
#{
reflection
.
quoted_table_name
}
.
#{
reflection
.
association_primary_key
}
"
relation
=
scoped
including
=
(
relation
.
eager_load_values
+
relation
.
includes_values
).
uniq
if
including
.
any?
join_dependency
=
ActiveRecord
::
Associations
::
JoinDependency
.
new
(
reflection
.
klass
,
including
,
[])
relation
=
join_dependency
.
join_associations
.
inject
(
relation
)
do
|
r
,
association
|
association
.
join_relation
(
r
)
end
end
relation
.
pluck
(
column
)
scoped
.
pluck
(
column
)
end
end
...
...
activerecord/lib/active_record/relation/calculations.rb
浏览文件 @
a7e715e1
...
...
@@ -107,7 +107,8 @@ def calculate(operation, column_name, options = {})
relation
=
with_default_scope
if
relation
.
equal?
(
self
)
if
eager_loading?
||
(
includes_values
.
present?
&&
references_eager_loaded_tables?
)
if
has_include?
(
column_name
)
construct_relation_for_association_calculations
.
calculate
(
operation
,
column_name
,
options
)
else
perform_calculation
(
operation
,
column_name
,
options
)
...
...
@@ -155,21 +156,25 @@ def pluck(column_name)
column_name
=
"
#{
table_name
}
.
#{
column_name
}
"
end
result
=
klass
.
connection
.
select_all
(
select
(
column_name
).
arel
,
nil
,
bind_values
)
if
has_include?
(
column_name
)
construct_relation_for_association_calculations
.
pluck
(
column_name
)
else
result
=
klass
.
connection
.
select_all
(
select
(
column_name
).
arel
,
nil
,
bind_values
)
key
=
result
.
columns
.
first
column
=
klass
.
column_types
.
fetch
(
key
)
{
result
.
column_types
.
fetch
(
key
)
{
Class
.
new
{
def
type_cast
(
v
);
v
;
end
}.
new
key
=
result
.
columns
.
first
column
=
klass
.
column_types
.
fetch
(
key
)
{
result
.
column_types
.
fetch
(
key
)
{
Class
.
new
{
def
type_cast
(
v
);
v
;
end
}.
new
}
}
}
result
.
map
do
|
attributes
|
raise
ArgumentError
,
"Pluck expects to select just one attribute:
#{
attributes
.
inspect
}
"
unless
attributes
.
one?
result
.
map
do
|
attributes
|
raise
ArgumentError
,
"Pluck expects to select just one attribute:
#{
attributes
.
inspect
}
"
unless
attributes
.
one?
value
=
klass
.
initialize_attributes
(
attributes
).
values
.
first
value
=
klass
.
initialize_attributes
(
attributes
).
values
.
first
column
.
type_cast
(
value
)
column
.
type_cast
(
value
)
end
end
end
...
...
@@ -185,6 +190,10 @@ def ids
private
def
has_include?
(
column_name
)
eager_loading?
||
(
includes_values
.
present?
&&
(
column_name
||
references_eager_loaded_tables?
))
end
def
perform_calculation
(
operation
,
column_name
,
options
=
{})
operation
=
operation
.
to_s
.
downcase
...
...
activerecord/test/cases/calculations_test.rb
浏览文件 @
a7e715e1
...
...
@@ -420,6 +420,40 @@ def test_maximum_with_from_option
Account
.
where
(
"credit_limit > 50"
).
from
(
'accounts'
).
maximum
(
:credit_limit
)
end
def
test_maximum_with_not_auto_table_name_prefix_if_column_included
Company
.
create!
(
:name
=>
"test"
,
:contracts
=>
[
Contract
.
new
(
:developer_id
=>
7
)])
# TODO: Investigate why PG isn't being typecast
if
current_adapter?
(
:PostgreSQLAdapter
)
assert_equal
"7"
,
Company
.
includes
(
:contracts
).
maximum
(
:developer_id
)
else
assert_equal
7
,
Company
.
includes
(
:contracts
).
maximum
(
:developer_id
)
end
end
def
test_minimum_with_not_auto_table_name_prefix_if_column_included
Company
.
create!
(
:name
=>
"test"
,
:contracts
=>
[
Contract
.
new
(
:developer_id
=>
7
)])
# TODO: Investigate why PG isn't being typecast
if
current_adapter?
(
:PostgreSQLAdapter
)
assert_equal
"7"
,
Company
.
includes
(
:contracts
).
minimum
(
:developer_id
)
else
assert_equal
7
,
Company
.
includes
(
:contracts
).
minimum
(
:developer_id
)
end
end
def
test_sum_with_not_auto_table_name_prefix_if_column_included
Company
.
create!
(
:name
=>
"test"
,
:contracts
=>
[
Contract
.
new
(
:developer_id
=>
7
)])
# TODO: Investigate why PG isn't being typecast
if
current_adapter?
(
:MysqlAdapter
)
||
current_adapter?
(
:PostgreSQLAdapter
)
assert_equal
"7"
,
Company
.
includes
(
:contracts
).
sum
(
:developer_id
)
else
assert_equal
7
,
Company
.
includes
(
:contracts
).
sum
(
:developer_id
)
end
end
def
test_from_option_with_specified_index
if
Edge
.
connection
.
adapter_name
==
'MySQL'
or
Edge
.
connection
.
adapter_name
==
'Mysql2'
assert_equal
Edge
.
count
(
:all
),
Edge
.
from
(
'edges USE INDEX(unique_edge_index)'
).
count
(
:all
)
...
...
@@ -477,6 +511,11 @@ def test_pluck_auto_table_name_prefix
assert_equal
[
c
.
id
],
Company
.
joins
(
:contracts
).
pluck
(
:id
)
end
def
test_pluck_if_table_included
c
=
Company
.
create!
(
:name
=>
"test"
,
:contracts
=>
[
Contract
.
new
(
:developer_id
=>
7
)])
assert_equal
[
c
.
id
],
Company
.
includes
(
:contracts
).
where
(
"contracts.id"
=>
c
.
contracts
.
first
).
pluck
(
:id
)
end
def
test_pluck_not_auto_table_name_prefix_if_column_joined
Company
.
create!
(
:name
=>
"test"
,
:contracts
=>
[
Contract
.
new
(
:developer_id
=>
7
)])
assert_equal
[
7
],
Company
.
joins
(
:contracts
).
pluck
(
:developer_id
)
...
...
@@ -500,4 +539,11 @@ def test_pluck_expects_a_single_selection
def
test_plucks_with_ids
assert_equal
Company
.
all
.
map
(
&
:id
).
sort
,
Company
.
ids
.
sort
end
def
test_pluck_not_auto_table_name_prefix_if_column_included
Company
.
create!
(
:name
=>
"test"
,
:contracts
=>
[
Contract
.
new
(
:developer_id
=>
7
)])
ids
=
Company
.
includes
(
:contracts
).
pluck
(
:developer_id
)
assert_equal
Company
.
count
,
ids
.
length
assert_equal
[
7
],
ids
.
compact
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录