Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
31d31fc2
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 搜索 >>
提交
31d31fc2
编写于
11月 02, 2019
作者:
P
pawurb
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Additionally order by primary key if implicit_order_column is not uniq
上级
9bfb73a5
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
42 addition
and
3 deletion
+42
-3
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+4
-0
activerecord/lib/active_record/model_schema.rb
activerecord/lib/active_record/model_schema.rb
+2
-2
activerecord/lib/active_record/relation/finder_methods.rb
activerecord/lib/active_record/relation/finder_methods.rb
+5
-1
activerecord/test/cases/finder_test.rb
activerecord/test/cases/finder_test.rb
+30
-0
activerecord/test/schema/schema.rb
activerecord/test/schema/schema.rb
+1
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
31d31fc2
*
Specifying
`implicit_order_column`
now subsorts the records by primary key if available to ensure deterministic results.
*Paweł Urbanek*
*
`where(attr => [])`
now loads an empty result without making a query.
*John Hawthorn*
...
...
activerecord/lib/active_record/model_schema.rb
浏览文件 @
31d31fc2
...
...
@@ -115,8 +115,8 @@ module ModelSchema
#
# Sets the column to sort records by when no explicit order clause is used
# during an ordered finder call. Useful when the primary key is not an
# auto-incrementing integer, for example when it's a UUID.
Note that using
#
a non-unique column can result in non-
deterministic results.
# auto-incrementing integer, for example when it's a UUID.
Records are subsorted
#
by the primary key if it exists to ensure
deterministic results.
included
do
mattr_accessor
:primary_key_prefix_type
,
instance_writer:
false
...
...
activerecord/lib/active_record/relation/finder_methods.rb
浏览文件 @
31d31fc2
...
...
@@ -560,7 +560,11 @@ def find_last(limit)
def
ordered_relation
if
order_values
.
empty?
&&
(
implicit_order_column
||
primary_key
)
order
(
arel_attribute
(
implicit_order_column
||
primary_key
).
asc
)
if
implicit_order_column
&&
primary_key
&&
implicit_order_column
!=
primary_key
order
(
arel_attribute
(
implicit_order_column
).
asc
,
arel_attribute
(
primary_key
).
asc
)
else
order
(
arel_attribute
(
implicit_order_column
||
primary_key
).
asc
)
end
else
self
end
...
...
activerecord/test/cases/finder_test.rb
浏览文件 @
31d31fc2
...
...
@@ -22,6 +22,7 @@
require
"models/car"
require
"models/tyre"
require
"models/subscriber"
require
"models/non_primary_key"
require
"support/stubs/strong_parameters"
class
FinderTest
<
ActiveRecord
::
TestCase
...
...
@@ -797,10 +798,39 @@ def test_implicit_order_column_is_configurable
assert_equal
topics
(
:fifth
),
Topic
.
first
assert_equal
topics
(
:third
),
Topic
.
last
c
=
Topic
.
connection
assert_sql
(
/ORDER BY
#{
Regexp
.
escape
(
c
.
quote_table_name
(
"topics.title"
))
}
DESC,
#{
Regexp
.
escape
(
c
.
quote_table_name
(
"topics.id"
))
}
DESC LIMIT/i
)
{
Topic
.
last
}
ensure
Topic
.
implicit_order_column
=
old_implicit_order_column
end
def
test_implicit_order_set_to_primary_key
old_implicit_order_column
=
Topic
.
implicit_order_column
Topic
.
implicit_order_column
=
"id"
c
=
Topic
.
connection
assert_sql
(
/ORDER BY
#{
Regexp
.
escape
(
c
.
quote_table_name
(
"topics.id"
))
}
DESC LIMIT/i
)
{
Topic
.
last
}
ensure
Topic
.
implicit_order_column
=
old_implicit_order_column
end
def
test_implicit_order_for_model_without_primary_key
old_implicit_order_column
=
NonPrimaryKey
.
implicit_order_column
NonPrimaryKey
.
implicit_order_column
=
"created_at"
c
=
NonPrimaryKey
.
connection
assert_sql
(
/ORDER BY
#{
Regexp
.
escape
(
c
.
quote_table_name
(
"non_primary_keys.created_at"
))
}
DESC LIMIT/i
)
{
NonPrimaryKey
.
last
}
ensure
NonPrimaryKey
.
implicit_order_column
=
old_implicit_order_column
end
def
test_take_and_first_and_last_with_integer_should_return_an_array
assert_kind_of
Array
,
Topic
.
take
(
5
)
assert_kind_of
Array
,
Topic
.
first
(
5
)
...
...
activerecord/test/schema/schema.rb
浏览文件 @
31d31fc2
...
...
@@ -1117,6 +1117,7 @@
create_table
:non_primary_keys
,
force:
true
,
id:
false
do
|
t
|
t
.
integer
:id
t
.
datetime
:created_at
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录