Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
13e35a59
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 搜索 >>
提交
13e35a59
编写于
3月 06, 2018
作者:
A
Andrew White
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #32119 from fegorodscy/master
Fix dependence on has_one/belongs_to relationships
上级
a3b7aa66
a286c32c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
58 addition
and
1 deletion
+58
-1
activerecord/lib/active_record/associations/belongs_to_association.rb
.../lib/active_record/associations/belongs_to_association.rb
+9
-1
activerecord/lib/active_record/associations/has_one_association.rb
...ord/lib/active_record/associations/has_one_association.rb
+1
-0
activerecord/test/cases/associations/belongs_to_associations_test.rb
...d/test/cases/associations/belongs_to_associations_test.rb
+24
-0
activerecord/test/cases/associations/has_one_associations_test.rb
...cord/test/cases/associations/has_one_associations_test.rb
+24
-0
未找到文件。
activerecord/lib/active_record/associations/belongs_to_association.rb
浏览文件 @
13e35a59
...
...
@@ -5,7 +5,15 @@ module Associations
# = Active Record Belongs To Association
class
BelongsToAssociation
<
SingularAssociation
#:nodoc:
def
handle_dependency
target
.
send
(
options
[
:dependent
])
if
load_target
return
unless
load_target
case
options
[
:dependent
]
when
:destroy
target
.
destroy
raise
ActiveRecord
::
Rollback
unless
target
.
destroyed?
else
target
.
send
(
options
[
:dependent
])
end
end
def
replace
(
record
)
...
...
activerecord/lib/active_record/associations/has_one_association.rb
浏览文件 @
13e35a59
...
...
@@ -60,6 +60,7 @@ def delete(method = options[:dependent])
when
:destroy
target
.
destroyed_by_association
=
reflection
target
.
destroy
throw
(
:abort
)
unless
target
.
destroyed?
when
:nullify
target
.
update_columns
(
reflection
.
foreign_key
=>
nil
)
if
target
.
persisted?
end
...
...
activerecord/test/cases/associations/belongs_to_associations_test.rb
浏览文件 @
13e35a59
...
...
@@ -931,6 +931,30 @@ def test_belongs_to_invalid_dependent_option_raises_exception
assert_equal
error
.
message
,
"The :dependent option must be one of [:destroy, :delete], but is :nullify"
end
class
DestroyableBook
<
ActiveRecord
::
Base
self
.
table_name
=
"books"
belongs_to
:author
,
class_name:
"UndestroyableAuthor"
,
dependent: :destroy
end
class
UndestroyableAuthor
<
ActiveRecord
::
Base
self
.
table_name
=
"authors"
has_one
:book
,
class_name:
"DestroyableBook"
,
foreign_key:
"author_id"
before_destroy
:dont
def
dont
throw
(
:abort
)
end
end
def
test_dependency_should_halt_parent_destruction
author
=
UndestroyableAuthor
.
create!
(
name:
"Test"
)
book
=
DestroyableBook
.
create!
(
author:
author
)
assert_no_difference
[
"UndestroyableAuthor.count"
,
"DestroyableBook.count"
]
do
assert_not
book
.
destroy
end
end
def
test_attributes_are_being_set_when_initialized_from_belongs_to_association_with_where_clause
new_firm
=
accounts
(
:signals37
).
build_firm
(
name:
"Apple"
)
assert_equal
new_firm
.
name
,
"Apple"
...
...
activerecord/test/cases/associations/has_one_associations_test.rb
浏览文件 @
13e35a59
...
...
@@ -725,4 +725,28 @@ class DestroyByParentAuthor < ActiveRecord::Base
assert_not
DestroyByParentBook
.
exists?
(
book
.
id
)
end
class
UndestroyableBook
<
ActiveRecord
::
Base
self
.
table_name
=
"books"
belongs_to
:author
,
class_name:
"DestroyableAuthor"
before_destroy
:dont
def
dont
throw
(
:abort
)
end
end
class
DestroyableAuthor
<
ActiveRecord
::
Base
self
.
table_name
=
"authors"
has_one
:book
,
class_name:
"UndestroyableBook"
,
foreign_key:
"author_id"
,
dependent: :destroy
end
def
test_dependency_should_halt_parent_destruction
author
=
DestroyableAuthor
.
create!
(
name:
"Test"
)
UndestroyableBook
.
create!
(
author:
author
)
assert_no_difference
[
"DestroyableAuthor.count"
,
"UndestroyableBook.count"
]
do
assert_not
author
.
destroy
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录