Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
2e379c1e
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,发现更多精彩内容 >>
提交
2e379c1e
编写于
5月 26, 2012
作者:
J
jeroeningen
提交者:
Carlos Antonio da Silva
6月 22, 2012
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ActiveRecord#pluck now accepts multiple columns
上级
8b173f3b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
51 addition
and
16 deletion
+51
-16
activerecord/lib/active_record/relation/calculations.rb
activerecord/lib/active_record/relation/calculations.rb
+35
-15
activerecord/test/cases/calculations_test.rb
activerecord/test/cases/calculations_test.rb
+16
-1
未找到文件。
activerecord/lib/active_record/relation/calculations.rb
浏览文件 @
2e379c1e
...
...
@@ -139,6 +139,12 @@ def calculate(operation, column_name, options = {})
# # SELECT people.id FROM people
# # => [1, 2, 3]
#
# Person.pluck([:id, :name])
# # SELECT people.id, people.name FROM people
#
# Person.pluck(:id, :name)
# # SELECT people.id, people.name FROM people
#
# Person.uniq.pluck(:role)
# # SELECT DISTINCT role FROM people
# # => ['admin', 'member', 'guest']
...
...
@@ -151,29 +157,43 @@ def calculate(operation, column_name, options = {})
# # SELECT DATEDIFF(updated_at, created_at) FROM people
# # => ['0', '27761', '173']
#
def
pluck
(
column_name
)
if
column_name
.
is_a?
(
Symbol
)
&&
column_names
.
include?
(
column_name
.
to_s
)
column_name
=
"
#{
table_name
}
.
#{
column_name
}
"
def
pluck
(
*
column_names
)
column_names
=
column_names
.
flatten
if
column_names
.
first
.
is_a?
(
Symbol
)
&&
self
.
column_names
.
include?
(
column_names
.
first
.
to_s
)
if
column_names
.
one?
column_names
=
"
#{
table_name
}
.
#{
column_names
.
first
}
"
else
column_names
=
column_names
.
collect
{
|
column_name
|
"
#{
table_name
}
.
#{
column_name
}
"
}
end
end
if
has_include?
(
column_name
)
construct_relation_for_association_calculations
.
pluck
(
column_name
)
if
has_include?
(
column_name
s
)
construct_relation_for_association_calculations
.
pluck
(
column_name
s
)
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
result
=
klass
.
connection
.
select_all
(
select
(
column_names
).
arel
,
nil
,
bind_values
)
keys
=
column_names
.
is_a?
(
Array
)
&&
!
column_names
.
one?
?
result
.
columns
:
[
result
.
columns
.
first
]
columns
=
keys
.
map
do
|
key
|
klass
.
column_types
.
fetch
(
key
)
{
result
.
column_types
.
fetch
(
key
)
{
Class
.
new
{
def
type_cast
(
v
);
v
;
end
}.
new
}
}
}
end
result
.
map
do
|
attributes
|
raise
ArgumentError
,
"Pluck expects to select just one attribute:
#{
attributes
.
inspect
}
"
unless
attributes
.
one?
if
attributes
.
one?
value
=
klass
.
initialize_attributes
(
attributes
).
values
.
first
value
=
klass
.
initialize_attributes
(
attributes
).
values
.
first
columns
.
first
.
type_cast
(
value
)
else
values
=
klass
.
initialize_attributes
(
attributes
).
values
column
.
type_cast
(
value
)
values
.
each_with_index
.
map
do
|
value
,
i
|
columns
[
i
].
type_cast
(
value
)
end
end
end
end
end
...
...
activerecord/test/cases/calculations_test.rb
浏览文件 @
2e379c1e
...
...
@@ -532,7 +532,7 @@ def test_pluck_with_selection_clause
assert_equal
[
50
+
53
+
55
+
60
],
Account
.
pluck
(
'SUM(DISTINCT(credit_limit)) as credit_limit'
)
end
def
test_pluck_expects_a_
single_selection
def
test_pluck_expects_a_
multiple_selection_as_array
assert_raise
(
ArgumentError
)
{
Account
.
pluck
'id, credit_limit'
}
end
...
...
@@ -546,4 +546,19 @@ def test_pluck_not_auto_table_name_prefix_if_column_included
assert_equal
Company
.
count
,
ids
.
length
assert_equal
[
7
],
ids
.
compact
end
def
test_pluck_multiple_columns
assert_equal
[
[
1
,
"The First Topic"
],
[
2
,
"The Second Topic of the day"
],
[
3
,
"The Third Topic of the day"
],
[
4
,
"The Fourth Topic of the day"
]
],
Topic
.
order
(
:id
).
pluck
([
:id
,
:title
])
assert_equal
[
[
1
,
"The First Topic"
,
"David"
],
[
2
,
"The Second Topic of the day"
,
"Mary"
],
[
3
,
"The Third Topic of the day"
,
"Carl"
],
[
4
,
"The Fourth Topic of the day"
,
"Carl"
]
],
Topic
.
order
(
:id
).
pluck
([
:id
,
:title
,
:author_name
])
assert_equal
[
[
1
,
"The First Topic"
],
[
2
,
"The Second Topic of the day"
],
[
3
,
"The Third Topic of the day"
],
[
4
,
"The Fourth Topic of the day"
]
],
Topic
.
order
(
:id
).
pluck
(
:id
,
:title
)
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录