Ruby on rails Rails回调启动器

Ruby on rails Rails回调启动器,ruby-on-rails,Ruby On Rails,我有一个与外部API模式匹配的ActiveRecord模式 我试图实现的是有条件地决定是否调用外部销毁API 我有这样的结构: class Car < ApplicationRecord has_one :spoiler, dependent: :destroy before_destroy: :destroy_external def destroy_external # API call to destroy Car. AUTOMATICALL

我有一个与外部API模式匹配的ActiveRecord模式

我试图实现的是有条件地决定是否调用外部销毁API

我有这样的结构:

class Car < ApplicationRecord
    has_one :spoiler, dependent: :destroy
    before_destroy: :destroy_external

    def destroy_external
        # API call to destroy Car. AUTOMATICALLY DESTROYS EXTERNAL SPOILER connected to Car
    end
end

class Spoiler < ApplicationRecord
    belongs_to :car
    before_destroy: :destroy_external

    def destroy_external
        # Call API destroy spoiler ONLY IF the callback was initiated directly and not via car destroy dependency
    end
end
它调用API来销毁外部扰流板,然后在本地销毁

但如果我想摧毁一辆汽车(本地和外部),我会:

我不希望在扰流板上执行destroy_external方法,也不希望检测到它是通过汽车回调调用的,并跳过API调用,因为API会自动销毁连接到汽车的扰流板,因此不需要销毁扰流板API


“黑客”是删除销毁依赖项并添加
spoiler.delete车内进行编码。销毁外部
方法,但这很难看。

我想,你需要
车的
模型中有一个:扰流板,从属::删除

它将从数据库中删除数据,但不会触发任何回调


我知道delete回调,直接从数据库中删除而不调用destroy回调,但是我在原始代码中犯了一个错误

使用destroy dependency时,使用:destroy on has_one和on has_多个关联

但是对于delete,我们使用:delete on has_one和:delete_all来表示has_多个关联

spoiler.destroy!
car.destroy!