Ruby on rails 我的自定义销毁方法不会触发销毁前后的默认回调
我正在编写一个插件,为模型提供绘图功能。删除操作是可拖动的操作,在发布删除操作之前,我并不总是希望删除源文件。所以我写了自己的销毁方法来帮助解决这个问题。除了不再触发Ruby on rails 我的自定义销毁方法不会触发销毁前后的默认回调,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我正在编写一个插件,为模型提供绘图功能。删除操作是可拖动的操作,在发布删除操作之前,我并不总是希望删除源文件。所以我写了自己的销毁方法来帮助解决这个问题。除了不再触发:销毁之前和:销毁之后的自定义回调之外,所有操作都完全按照我的要求进行 关于如何: 重新绑定对销毁方法的回调 工作一些别名\u方法\u链巫毒 获取模型回调的列表,这样我就可以称之为手动 用另一种方法解决这个问题 以下是我的销毁方法: def destroy if self.attribute_names.include?
:销毁之前
和:销毁之后
的自定义回调之外,所有操作都完全按照我的要求进行
关于如何:
def destroy
if self.attribute_names.include?('draft') && self.skip_draft == false
if handle_destroy # if true is returned
super # go ahead and destroy as normal
end
else
super
end
end
更新:我刚刚发现:
,但这似乎提议的技术也不能适应回调。有没有办法既吃蛋糕又吃蛋糕?我错了,调用super时没有调用回调。我最终依赖于我最初发布的代码。我更改了handle\u destroy方法的返回方式 我将向您展示如何在您希望显式启动回调时启动回调
def destroy
if self.attribute_names.include?('draft') && self.skip_draft == false
if handle_destroy # if true is returned
super # go ahead and destroy as normal
else
# Execute all custom callbacks that are not dependent type callbacks (ie: if the callback method name contains "dependent")
# Dependent callbacks delete records and this is not what the drafting system is all about.
(self.class.before_destroy_callback_chain + self.class.after_destroy_callback_chain).each do |cb|
unless (cb.method.kind_of?(Symbol) && cb.method.to_s.match(/dependent/))
cb.call(self)
end
end
end
else
# normal delete
super
end
end