Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
c82c5f8f
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 搜索 >>
提交
c82c5f8f
编写于
9月 05, 2015
作者:
W
Wojciech Wnętrzak
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Deprecate passing conditions to AR::Relation destroy_all and delete_all methods
上级
a8f4568f
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
29 addition
and
15 deletion
+29
-15
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+5
-0
activerecord/lib/active_record/relation.rb
activerecord/lib/active_record/relation.rb
+10
-13
activerecord/test/cases/persistence_test.rb
activerecord/test/cases/persistence_test.rb
+1
-1
activerecord/test/cases/relations_test.rb
activerecord/test/cases/relations_test.rb
+12
-0
activerecord/test/models/topic.rb
activerecord/test/models/topic.rb
+1
-1
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
c82c5f8f
*
Deprecate passing conditions to
`ActiveRecord::Relation#delete_all`
and
`ActiveRecord::Relation#destroy_all`
*Wojciech Wnętrzak*
*
PostgreSQL,
`create_schema`
,
`drop_schema`
and
`rename_table`
now quote
schema names.
...
...
activerecord/lib/active_record/relation.rb
浏览文件 @
c82c5f8f
...
...
@@ -418,7 +418,7 @@ def update(id = :all, attributes)
end
end
# Destroys the records
matching +conditions+
by instantiating each
# Destroys the records by instantiating each
# record and calling its +destroy+ method. Each object's callbacks are
# executed (including <tt>:dependent</tt> association options). Returns the
# collection of objects that were destroyed; each will be frozen, to
...
...
@@ -431,20 +431,15 @@ def update(id = :all, attributes)
# rows quickly, without concern for their associations or callbacks, use
# +delete_all+ instead.
#
# ==== Parameters
#
# * +conditions+ - A string, array, or hash that specifies which records
# to destroy. If omitted, all records are destroyed. See the
# Conditions section in the introduction to ActiveRecord::Base for
# more information.
#
# ==== Examples
#
# Person.destroy_all("last_login < '2004-04-04'")
# Person.destroy_all(status: "inactive")
# Person.where(age: 0..18).destroy_all
def
destroy_all
(
conditions
=
nil
)
if
conditions
ActiveSupport
::
Deprecation
.
warn
(
<<-
MESSAGE
.
squish
)
Passing conditions to destroy_all is deprecated and will be removed in Rails 5.1.
To achieve the same use where(conditions).destroy_all
MESSAGE
where
(
conditions
).
destroy_all
else
to_a
.
each
(
&
:destroy
).
tap
{
reset
}
...
...
@@ -478,15 +473,13 @@ def destroy(id)
end
end
# Deletes the records
matching +conditions+
without instantiating the records
# Deletes the records without instantiating the records
# first, and hence not calling the +destroy+ method nor invoking callbacks. This
# is a single SQL DELETE statement that goes straight to the database, much more
# efficient than +destroy_all+. Be careful with relations though, in particular
# <tt>:dependent</tt> rules defined on associations are not honored. Returns the
# number of rows affected.
#
# Post.delete_all("person_id = 5 AND (category = 'Something' OR category = 'Else')")
# Post.delete_all(["person_id = ? AND (category = ? OR category = ?)", 5, 'Something', 'Else'])
# Post.where(person_id: 5).where(category: ['Something', 'Else']).delete_all
#
# Both calls delete the affected posts all at once with a single DELETE statement.
...
...
@@ -512,6 +505,10 @@ def delete_all(conditions = nil)
end
if
conditions
ActiveSupport
::
Deprecation
.
warn
(
<<-
MESSAGE
.
squish
)
Passing conditions to delete_all is deprecated and will be removed in Rails 5.1.
To achieve the same use where(conditions).delete_all
MESSAGE
where
(
conditions
).
delete_all
else
stmt
=
Arel
::
DeleteManager
.
new
...
...
activerecord/test/cases/persistence_test.rb
浏览文件 @
c82c5f8f
...
...
@@ -126,7 +126,7 @@ def test_destroy_all
assert
!
topics_by_mary
.
empty?
assert_difference
(
'Topic.count'
,
-
topics_by_mary
.
size
)
do
destroyed
=
Topic
.
destroy_all
(
conditions
)
.
sort_by
(
&
:id
)
destroyed
=
Topic
.
where
(
conditions
).
destroy_all
.
sort_by
(
&
:id
)
assert_equal
topics_by_mary
,
destroyed
assert
destroyed
.
all?
(
&
:frozen?
),
"destroyed topics should be frozen"
end
...
...
activerecord/test/cases/relations_test.rb
浏览文件 @
c82c5f8f
...
...
@@ -931,6 +931,12 @@ def test_destroy_all
assert
davids
.
loaded?
end
def
test_destroy_all_with_conditions_is_deprecated
assert_deprecated
do
assert_difference
(
'Author.count'
,
-
1
)
{
Author
.
destroy_all
(
name:
'David'
)
}
end
end
def
test_delete_all
davids
=
Author
.
where
(
:name
=>
'David'
)
...
...
@@ -938,6 +944,12 @@ def test_delete_all
assert
!
davids
.
loaded?
end
def
test_delete_all_with_conditions_is_deprecated
assert_deprecated
do
assert_difference
(
'Author.count'
,
-
1
)
{
Author
.
delete_all
(
name:
'David'
)
}
end
end
def
test_delete_all_loaded
davids
=
Author
.
where
(
:name
=>
'David'
)
...
...
activerecord/test/models/topic.rb
浏览文件 @
c82c5f8f
...
...
@@ -86,7 +86,7 @@ def default_written_on
end
def
destroy_children
self
.
class
.
delete_all
"parent_id =
#{
id
}
"
self
.
class
.
where
(
"parent_id =
#{
id
}
"
).
delete_all
end
def
set_email_address
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录