Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ae032ec3
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 搜索 >>
提交
ae032ec3
编写于
11月 20, 2017
作者:
N
Nikita Misharin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Provide arguments to RecordNotFound
上级
f27319a7
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
35 addition
and
7 deletion
+35
-7
activerecord/lib/active_record/associations/collection_association.rb
.../lib/active_record/associations/collection_association.rb
+7
-1
activerecord/lib/active_record/relation/finder_methods.rb
activerecord/lib/active_record/relation/finder_methods.rb
+9
-5
activerecord/test/cases/associations/inverse_associations_test.rb
...cord/test/cases/associations/inverse_associations_test.rb
+4
-1
activerecord/test/cases/finder_test.rb
activerecord/test/cases/finder_test.rb
+15
-0
未找到文件。
activerecord/lib/active_record/associations/collection_association.rb
浏览文件 @
ae032ec3
...
...
@@ -79,7 +79,13 @@ def reset
def
find
(
*
args
)
if
options
[
:inverse_of
]
&&
loaded?
args_flatten
=
args
.
flatten
raise
RecordNotFound
,
"Couldn't find
#{
scope
.
klass
.
name
}
without an ID"
if
args_flatten
.
blank?
model
=
scope
.
klass
if
args_flatten
.
blank?
error_message
=
"Couldn't find
#{
model
.
name
}
without an ID"
raise
RecordNotFound
.
new
(
error_message
,
model
.
name
,
model
.
primary_key
,
args
)
end
result
=
find_by_scan
(
*
args
)
result_size
=
Array
(
result
).
size
...
...
activerecord/lib/active_record/relation/finder_methods.rb
浏览文件 @
ae032ec3
...
...
@@ -88,7 +88,7 @@ def find_by!(arg, *args)
where
(
arg
,
*
args
).
take!
rescue
::
RangeError
raise
RecordNotFound
.
new
(
"Couldn't find
#{
@klass
.
name
}
with an out of range value"
,
@klass
.
name
)
@klass
.
name
,
@klass
.
primary_key
)
end
# Gives a record (or N records if a parameter is supplied) without any implied
...
...
@@ -339,7 +339,7 @@ def raise_record_not_found_exception!(ids = nil, result_size = nil, expected_siz
if
ids
.
nil?
error
=
"Couldn't find
#{
name
}
"
.
dup
error
<<
" with
#{
conditions
}
"
if
conditions
raise
RecordNotFound
.
new
(
error
,
name
)
raise
RecordNotFound
.
new
(
error
,
name
,
key
)
elsif
Array
(
ids
).
size
==
1
error
=
"Couldn't find
#{
name
}
with '
#{
key
}
'=
#{
ids
}#{
conditions
}
"
raise
RecordNotFound
.
new
(
error
,
name
,
key
,
ids
)
...
...
@@ -347,7 +347,7 @@ def raise_record_not_found_exception!(ids = nil, result_size = nil, expected_siz
error
=
"Couldn't find all
#{
name
.
pluralize
}
with '
#{
key
}
': "
.
dup
error
<<
"(
#{
ids
.
join
(
", "
)
}
)
#{
conditions
}
(found
#{
result_size
}
results, but was looking for
#{
expected_size
}
)."
error
<<
" Couldn't find
#{
name
.
pluralize
(
not_found_ids
.
size
)
}
with
#{
key
.
to_s
.
pluralize
(
not_found_ids
.
size
)
}
#{
not_found_ids
.
join
(
', '
)
}
."
if
not_found_ids
raise
RecordNotFound
.
new
(
error
,
name
,
primary_
key
,
ids
)
raise
RecordNotFound
.
new
(
error
,
name
,
key
,
ids
)
end
end
...
...
@@ -433,9 +433,12 @@ def find_with_ids(*ids)
ids
=
ids
.
flatten
.
compact
.
uniq
model_name
=
@klass
.
name
case
ids
.
size
when
0
raise
RecordNotFound
,
"Couldn't find
#{
@klass
.
name
}
without an ID"
error_message
=
"Couldn't find
#{
model_name
}
without an ID"
raise
RecordNotFound
.
new
(
error_message
,
model_name
,
primary_key
)
when
1
result
=
find_one
(
ids
.
first
)
expects_array
?
[
result
]
:
result
...
...
@@ -443,7 +446,8 @@ def find_with_ids(*ids)
find_some
(
ids
)
end
rescue
::
RangeError
raise
RecordNotFound
,
"Couldn't find
#{
@klass
.
name
}
with an out of range ID"
error_message
=
"Couldn't find
#{
model_name
}
with an out of range ID"
raise
RecordNotFound
.
new
(
error_message
,
model_name
,
primary_key
,
ids
)
end
def
find_one
(
id
)
...
...
activerecord/test/cases/associations/inverse_associations_test.rb
浏览文件 @
ae032ec3
...
...
@@ -484,7 +484,10 @@ def test_raise_record_not_found_error_when_invalid_ids_are_passed
def
test_raise_record_not_found_error_when_no_ids_are_passed
man
=
Man
.
create!
assert_raise
(
ActiveRecord
::
RecordNotFound
)
{
man
.
interests
.
find
()
}
exception
=
assert_raise
(
ActiveRecord
::
RecordNotFound
)
{
man
.
interests
.
load
.
find
()
}
assert_equal
exception
.
model
,
"Interest"
assert_equal
exception
.
primary_key
,
"id"
end
def
test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
...
...
activerecord/test/cases/finder_test.rb
浏览文件 @
ae032ec3
...
...
@@ -120,6 +120,21 @@ def test_find_with_ids_and_offset
assert_equal
"The Fourth Topic of the day"
,
records
[
2
].
title
end
def
test_find_with_ids_with_no_id_passed
exception
=
assert_raises
(
ActiveRecord
::
RecordNotFound
)
{
Topic
.
find
}
assert_equal
exception
.
model
,
"Topic"
assert_equal
exception
.
primary_key
,
"id"
end
def
test_find_with_ids_with_id_out_of_range
exception
=
assert_raises
(
ActiveRecord
::
RecordNotFound
)
do
Topic
.
find
(
"9999999999999999999999999999999"
)
end
assert_equal
exception
.
model
,
"Topic"
assert_equal
exception
.
primary_key
,
"id"
end
def
test_find_passing_active_record_object_is_not_permitted
assert_raises
(
ArgumentError
)
do
Topic
.
find
(
Topic
.
last
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录