restrict_with_exception 和 restrict_with_error 之间的区别
Difference between restrict_with_exception and restrict_with_error
谁能告诉我这两种方式在处理父键被销毁的对象时的区别?是什么实际原因让您二选一?
那些是依赖选项
依赖选项是什么?
如果 Rails 的模型有 child 记录,删除 parent 记录时,依赖选项决定如何处理 child 记录.
restrict_with_exception
:restrict_with_exception
– 如果有任何关联记录,将引发异常。
:restrict_with_exception
- 如果有child条记录,那么你ActiveRecord::DeleteRestrictionError
会遇到
restrict_with_error
:restrict_with_error
– 如果有任何关联的记录,将向所有者(您试图删除的记录)添加一个错误。
:restrict_with_error
- 如果有child记录,不能删除,错误信息添加到parent记录。
除
之外的几个选项
:destroy
- 删除 child 条 parent 条记录。
:delete_all
- 删除 child 条 parent 条记录。但是由于直接删除了DB的记录,所以没有执行child记录的回调处理。
:nullify
NULL
- 更新 child 记录的外键。
您还可以google获得更多
restrict_with_exception
如果有任何关联的记录,将引发异常:
class Student< ActiveRecord::Base
has_many :courses, dependent: :restrict_with_exception
has_many :books
end
restrict_with_error
如果有任何关联的记录,将向所有者(您尝试删除的记录)添加一个错误:
class Foo < ActiveRecord::Base
has_many :bars, dependent: :restrict_with_error
end
预期行为
对于标准验证,错误消息包含翻译,错误详细信息包含密钥,此处为空白错误:
f1 = Foo.new
f1.save!
#=> ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
f1.errors
#=> #<ActiveModel::Errors:0x007fb666637af8
#=> @base=#<Foo:0x007fb6666ddbb0 id: nil, name: nil>,
#=> @details={:name=>[{:error=>:blank}], :type=>[{:error=>:blank}]},
#=> @messages={:name=>["can't be blank"], :type=>["can't be blank"]}>
来自fool-dev的回答
You can also google for more
不需要这样做。来自 Active Record
源代码:
def self.valid_dependent_options
[:destroy, :delete_all, :nullify, :restrict_with_error, :restrict_with_exception, :destroy_async]
end
所以,列表几乎完成了,只剩下 :destroy_async
到 google。这是它的作用,来自文档:
If set to :destroy_async
, the associated object is scheduled to be destroyed in a background job.
谁能告诉我这两种方式在处理父键被销毁的对象时的区别?是什么实际原因让您二选一?
那些是依赖选项
依赖选项是什么?
如果 Rails 的模型有 child 记录,删除 parent 记录时,依赖选项决定如何处理 child 记录.
restrict_with_exception
:restrict_with_exception
– 如果有任何关联记录,将引发异常。
:restrict_with_exception
- 如果有child条记录,那么你ActiveRecord::DeleteRestrictionError
会遇到
restrict_with_error
:restrict_with_error
– 如果有任何关联的记录,将向所有者(您试图删除的记录)添加一个错误。
:restrict_with_error
- 如果有child记录,不能删除,错误信息添加到parent记录。
除
之外的几个选项:destroy
- 删除 child 条 parent 条记录。
:delete_all
- 删除 child 条 parent 条记录。但是由于直接删除了DB的记录,所以没有执行child记录的回调处理。
:nullify
NULL
- 更新 child 记录的外键。
您还可以google获得更多
restrict_with_exception
如果有任何关联的记录,将引发异常:
class Student< ActiveRecord::Base
has_many :courses, dependent: :restrict_with_exception
has_many :books
end
restrict_with_error
如果有任何关联的记录,将向所有者(您尝试删除的记录)添加一个错误:
class Foo < ActiveRecord::Base
has_many :bars, dependent: :restrict_with_error
end
预期行为
对于标准验证,错误消息包含翻译,错误详细信息包含密钥,此处为空白错误:
f1 = Foo.new
f1.save!
#=> ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
f1.errors
#=> #<ActiveModel::Errors:0x007fb666637af8
#=> @base=#<Foo:0x007fb6666ddbb0 id: nil, name: nil>,
#=> @details={:name=>[{:error=>:blank}], :type=>[{:error=>:blank}]},
#=> @messages={:name=>["can't be blank"], :type=>["can't be blank"]}>
来自fool-dev的回答
You can also google for more
不需要这样做。来自 Active Record
源代码:
def self.valid_dependent_options
[:destroy, :delete_all, :nullify, :restrict_with_error, :restrict_with_exception, :destroy_async]
end
所以,列表几乎完成了,只剩下 :destroy_async
到 google。这是它的作用,来自文档:
If set to
:destroy_async
, the associated object is scheduled to be destroyed in a background job.