Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
9f3730a5
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,发现更多精彩内容 >>
提交
9f3730a5
编写于
1月 28, 2016
作者:
R
Rafael França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #16400 from bogdan/last-with-sql
Always use SQL limit in Relation#last when limit argument given
上级
2637fb75
7705fcf0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
60 addition
and
31 deletion
+60
-31
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+20
-0
activerecord/lib/active_record/relation/finder_methods.rb
activerecord/lib/active_record/relation/finder_methods.rb
+17
-22
activerecord/test/cases/finder_test.rb
activerecord/test/cases/finder_test.rb
+23
-9
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
9f3730a5
*
Rework
`ActiveRecord::Relation#last`
1.
Always find last with ruby if relation is loaded
2.
Always use SQL instead if relation is not loaded.
3.
Deprecated relation loading when SQL order can not be automatically reversed
Topic.order("title").load.last(3)
# before: SELECT ...
# after: No SQL
Topic.order("title").last
# before: SELECT * FROM `topics`
# after: SELECT * FROM `topics` ORDER BY `topics`.`title` DESC LIMIT 1
Topic.order("coalesce(author, title)").last
# before: SELECT * FROM `topics`
# after: Deprecation Warning for irreversible order
*Bogdan Gusiev*
*
`ActiveRecord::Relation#reverse_order`
throws
`ActiveRecord::IrreversibleOrderError`
when the order can not be reversed using current trivial algorithm.
Also raises the same error when
`#reverse_order`
is called on
...
...
activerecord/lib/active_record/relation/finder_methods.rb
浏览文件 @
9f3730a5
...
...
@@ -145,15 +145,23 @@ def first!
#
# [#<Person id:4>, #<Person id:3>, #<Person id:2>]
def
last
(
limit
=
nil
)
if
limit
if
order_values
.
empty?
&&
primary_key
order
(
arel_table
[
primary_key
].
desc
).
limit
(
limit
).
reverse
else
to_a
.
last
(
limit
)
end
else
find_last
end
return
find_last
(
limit
)
if
loaded?
result
=
order_values
.
empty?
&&
primary_key
?
order
(
arel_table
[
primary_key
].
desc
)
:
reverse_order
result
=
result
.
limit!
(
limit
||
1
)
limit
?
result
.
reverse
:
result
.
first
rescue
ActiveRecord
::
IrreversibleOrderError
ActiveSupport
::
Deprecation
.
warn
(
<<-
WARNING
.
squish
)
Finding a last element by loading the relation when SQL ORDER
can not be reversed is deprecated.
Rails 5.1 will raise ActiveRecord::IrreversibleOrderError in this case.
Please call `to_a.last` if you still want to load the relation.
WARNING
find_last
(
limit
)
end
def
find_last
(
limit
)
limit
?
to_a
.
last
(
limit
)
:
to_a
.
last
end
# Same as #last but raises ActiveRecord::RecordNotFound if no record
...
...
@@ -523,19 +531,6 @@ def find_nth_with_limit(index, limit)
relation
.
limit
(
limit
).
to_a
end
def
find_last
if
loaded?
@records
.
last
else
@last
||=
if
limit_value
to_a
.
last
else
reverse_order
.
limit
(
1
).
to_a
.
first
end
end
end
private
def
find_nth_with_limit_and_offset
(
index
,
limit
,
offset
:)
# :nodoc:
...
...
activerecord/test/cases/finder_test.rb
浏览文件 @
9f3730a5
...
...
@@ -506,7 +506,7 @@ def test_model_class_responds_to_last_bang
end
end
def
test_take_and_first_and_last_with_integer_should_use_sql
_limit
def
test_take_and_first_and_last_with_integer_should_use_sql
assert_sql
(
/LIMIT|ROWNUM <=/
)
{
Topic
.
take
(
3
).
entries
}
assert_sql
(
/LIMIT|ROWNUM <=/
)
{
Topic
.
first
(
2
).
entries
}
assert_sql
(
/LIMIT|ROWNUM <=/
)
{
Topic
.
last
(
5
).
entries
}
...
...
@@ -516,16 +516,30 @@ def test_last_with_integer_and_order_should_keep_the_order
assert_equal
Topic
.
order
(
"title"
).
to_a
.
last
(
2
),
Topic
.
order
(
"title"
).
last
(
2
)
end
def
test_last_with_integer_and_order_should_
not_use_sql_limit
query
=
assert_sql
{
Topic
.
order
(
"title"
).
last
(
5
).
entries
}
assert_
equal
1
,
query
.
length
assert
_no_match
(
/LIMIT/
,
query
.
first
)
def
test_last_with_integer_and_order_should_
use_sql
relation
=
Topic
.
order
(
"title"
)
assert_
queries
(
1
)
{
relation
.
last
(
5
)
}
assert
!
relation
.
loaded?
end
def
test_last_with_integer_and_reorder_should_not_use_sql_limit
query
=
assert_sql
{
Topic
.
reorder
(
"title"
).
last
(
5
).
entries
}
assert_equal
1
,
query
.
length
assert_no_match
(
/LIMIT/
,
query
.
first
)
def
test_last_with_integer_and_reorder_should_use_sql
relation
=
Topic
.
reorder
(
"title"
)
assert_queries
(
1
)
{
relation
.
last
(
5
)
}
assert
!
relation
.
loaded?
end
def
test_last_on_loaded_relation_should_not_use_sql
relation
=
Topic
.
limit
(
10
).
load
assert_no_queries
do
relation
.
last
relation
.
last
(
2
)
end
end
def
test_last_with_irreversible_order
assert_deprecated
do
Topic
.
order
(
"coalesce(author_name, title)"
).
last
end
end
def
test_take_and_first_and_last_with_integer_should_return_an_array
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录