扮演偏执狂:真正销毁带有偏执狂孩子的唱片

时间:2018-08-08 15:37:31

标签: ruby-on-rails acts-as-paranoid

我正在使用ActAsParanoid gem软删除一些记录,比如说孩子。父母不是偏执狂,我希望在销毁父母时,孩子真的被摧毁。

class Parent < ApplicationRecord
  has_many :children, dependent: :destroy
end

class Child < ApplicationRecord
  act_as_paranoid 
  belongs_to :parent
end

我现在看到子级被软删除,防止了父级被破坏,因为子级中的引用变得无效。在父母被摧毁之后,我该如何使孩子真正被摧毁? (我想知道是否可以避免自定义回调)

更新:

如果有一个先前删除的子级,则使用dependent: :delete_all会给出相同的错误。

1 个答案:

答案 0 :(得分:1)

实际上,Paranoia中没有考虑您的案件。如果您看到该文档,则有examples用于其他情况,但不是您想要的情况。

dependent关联中的

has_many属性仅接受destroydelete_allnullifyrestrict_with_exceptionrestrict_with_error参见here。所以你不能在那里打really_destroy!

幸运的是,您可以使用一个before_destroy回调函数在每个子级中执行really_destroy!。尝试这样的事情:

class Parent < ApplicationRecord
  has_many :children

  before_destroy :really_destroy_children!

  private

  def really_destroy_children!
    children.with_deleted.each do |child|
      child.really_destroy!
    end
  end

end