Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
53d5a682
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 搜索 >>
未验证
提交
53d5a682
编写于
12月 04, 2018
作者:
R
Ryuta Kamizono
提交者:
GitHub
12月 04, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #34607 from gmcgibbon/clear_scope_on_delete
Reset scope after collection delete
上级
6ca6478a
67bca35a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
72 addition
and
4 deletion
+72
-4
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+4
-0
activerecord/lib/active_record/associations/collection_proxy.rb
...record/lib/active_record/associations/collection_proxy.rb
+4
-4
activerecord/test/cases/associations/has_many_associations_test.rb
...ord/test/cases/associations/has_many_associations_test.rb
+32
-0
activerecord/test/cases/associations/has_many_through_associations_test.rb
.../cases/associations/has_many_through_associations_test.rb
+32
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
53d5a682
*
Reset scope after delete on collection association to clear stale offsets of removed records.
*Gannon McGibbon*
*
Add the ability to prevent writes to a database for the duration of a block.
Allows the application to prevent writes to a database. This can be useful when
...
...
activerecord/lib/active_record/associations/collection_proxy.rb
浏览文件 @
53d5a682
...
...
@@ -500,7 +500,7 @@ def replace(other_array)
# Pet.find(1, 2, 3)
# # => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (1, 2, 3)
def
delete_all
(
dependent
=
nil
)
@association
.
delete_all
(
dependent
)
@association
.
delete_all
(
dependent
)
.
tap
{
reset_scope
}
end
# Deletes the records of the collection directly from the database
...
...
@@ -527,7 +527,7 @@ def delete_all(dependent = nil)
#
# Pet.find(1) # => Couldn't find Pet with id=1
def
destroy_all
@association
.
destroy_all
@association
.
destroy_all
.
tap
{
reset_scope
}
end
# Deletes the +records+ supplied from the collection according to the strategy
...
...
@@ -646,7 +646,7 @@ def destroy_all
# # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
# # ]
def
delete
(
*
records
)
@association
.
delete
(
*
records
)
@association
.
delete
(
*
records
)
.
tap
{
reset_scope
}
end
# Destroys the +records+ supplied and removes them from the collection.
...
...
@@ -718,7 +718,7 @@ def delete(*records)
#
# Pet.find(4, 5, 6) # => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (4, 5, 6)
def
destroy
(
*
records
)
@association
.
destroy
(
*
records
)
@association
.
destroy
(
*
records
)
.
tap
{
reset_scope
}
end
##
...
...
activerecord/test/cases/associations/has_many_associations_test.rb
浏览文件 @
53d5a682
...
...
@@ -294,6 +294,14 @@ def test_delete_all_on_association_with_nil_dependency_is_the_same_as_not_loaded
assert_equal
(
expected_sql
,
loaded_sql
)
end
def
test_delete_all_on_association_clears_scope
author
=
Author
.
create!
(
name:
"Gannon"
)
posts
=
author
.
posts
posts
.
create!
(
title:
"test"
,
body:
"body"
)
posts
.
delete_all
assert_nil
posts
.
first
end
def
test_building_the_associated_object_with_implicit_sti_base_class
firm
=
DependentFirm
.
new
company
=
firm
.
companies
.
build
...
...
@@ -1710,6 +1718,30 @@ def test_destroy_all
assert
companies
(
:first_firm
).
clients_of_firm
.
reload
.
empty?
,
"37signals has no clients after destroy all and refresh"
end
def
test_destroy_all_on_association_clears_scope
author
=
Author
.
create!
(
name:
"Gannon"
)
posts
=
author
.
posts
posts
.
create!
(
title:
"test"
,
body:
"body"
)
posts
.
destroy_all
assert_nil
posts
.
first
end
def
test_destroy_on_association_clears_scope
author
=
Author
.
create!
(
name:
"Gannon"
)
posts
=
author
.
posts
post
=
posts
.
create!
(
title:
"test"
,
body:
"body"
)
posts
.
destroy
(
post
)
assert_nil
posts
.
first
end
def
test_delete_on_association_clears_scope
author
=
Author
.
create!
(
name:
"Gannon"
)
posts
=
author
.
posts
post
=
posts
.
create!
(
title:
"test"
,
body:
"body"
)
posts
.
delete
(
post
)
assert_nil
posts
.
first
end
def
test_dependence
firm
=
companies
(
:first_firm
)
assert_equal
3
,
firm
.
clients
.
size
...
...
activerecord/test/cases/associations/has_many_through_associations_test.rb
浏览文件 @
53d5a682
...
...
@@ -227,6 +227,14 @@ def test_delete_all_for_with_dependent_option_delete_all
end
end
def
test_delete_all_on_association_clears_scope
post
=
Post
.
create!
(
title:
"Rails 6"
,
body:
""
)
people
=
post
.
people
people
.
create!
(
first_name:
"Jeb"
)
people
.
delete_all
assert_nil
people
.
first
end
def
test_concat
person
=
people
(
:david
)
post
=
posts
(
:thinking
)
...
...
@@ -401,6 +409,30 @@ def test_destroy_all
assert_empty
posts
(
:welcome
).
people
.
reload
end
def
test_destroy_all_on_association_clears_scope
post
=
Post
.
create!
(
title:
"Rails 6"
,
body:
""
)
people
=
post
.
people
people
.
create!
(
first_name:
"Jeb"
)
people
.
destroy_all
assert_nil
people
.
first
end
def
test_destroy_on_association_clears_scope
post
=
Post
.
create!
(
title:
"Rails 6"
,
body:
""
)
people
=
post
.
people
person
=
people
.
create!
(
first_name:
"Jeb"
)
people
.
destroy
(
person
)
assert_nil
people
.
first
end
def
test_delete_on_association_clears_scope
post
=
Post
.
create!
(
title:
"Rails 6"
,
body:
""
)
people
=
post
.
people
person
=
people
.
create!
(
first_name:
"Jeb"
)
people
.
delete
(
person
)
assert_nil
people
.
first
end
def
test_should_raise_exception_for_destroying_mismatching_records
assert_no_difference
[
"Person.count"
,
"Reader.count"
]
do
assert_raise
(
ActiveRecord
::
AssociationTypeMismatch
)
{
posts
(
:welcome
).
people
.
destroy
(
posts
(
:thinking
))
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录