简体   繁体   English

有条件覆盖ActiveRecord删除机制的最佳方法是什么?

[英]What's the best way to conditionally override ActiveRecord's deletion mechanism?

I am trying to hijack Rails' deletion mechanism to make it behave differently for a certain set of models. 我正在尝试劫持Rails的删除机制,以使其对某些模型的行为有所不同。

Both ActiveRecord::Base#delete and #destroy lead back to ActiveRecord::Relation#delete_all , so it would make sense to override this method. ActiveRecord::Base#delete#destroy返回到ActiveRecord::Relation#delete_all ,因此可以重写此方法。

I have tried... 我努力了...

class MyModel < ActiveRecord::Base
  class << all
    def delete_all
      "My destruction mechanism"
    end
  end
end

... but ::all is a method that returns a different object every time... ...但是::all是每次都会返回不同对象的方法...

class MyModel < ActiveRecord::Base
  def self.all
    super.tap do |obj|
      class << obj
        def delete_all
          "My destruction mechanism"
        end
      end
    end
  end
end

... but ::all isn't the only scope that needs it overridden anyway... ...但是::all并不是唯一需要覆盖它的作用域...

class ActiveRecord::Relation
  def delete_all(*args)
    "My destruction mechanism"
  end
end

... but it can only apply to MyModel and its subclasses... ...但是它只能应用于MyModel及其子类...

class ActiveRecord::Relation
  def delete_all(*args)
    if @klass.new.is_a?(MyModel)
      "My destruction mechanism"
    else
      super
    end
  end
end

... but this causes stack overflows on other models. ...但是这会导致其他模型上的堆栈溢出。

Help? 救命?

Overriding delete and destroy on your model should accomplish most of what you want. 在模型上重写deletedestroy应该可以完成您想要的大部分工作。 Check out how the Paranoia gem accomplishes its overriding. 了解Paranoia宝石如何实现其压倒性。 The library's only 200 lines or so, and also handles associated models (eg when you have dependent: :destroy ). 该库只有200行左右,并且还可以处理关联的模型(例如,当您具有dependent: :destroy )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在子类(例如after_create)中覆盖Rails ActiveRecord创建/删除事件的正确方法是什么? - What's the proper way to override Rails ActiveRecord creation/deletion events in a subclass, such as after_create 保护某些记录免受修改或删除的最佳方法是什么? - What's the best way to protect certain records from modification or deletion? 跨两个ActiveRecord行镜像值的最佳方法是什么? - What's the best way to mirror a value across two ActiveRecord rows? 保存ActiveRecord模型实例的最佳方法是什么? - What's the best way to save an ActiveRecord model instance? 使用Rails和ActiveRecord维护记录的编辑历史记录的最佳方法是什么 - What is the best way to maintain a record's edit history with Rails and ActiveRecord 将ActiveRecord模型与版本以及它们与版本的关联存储的最佳方法是什么? - What's the best way to store the ActiveRecord Models with Versions and their Associations with Versions? 什么是覆盖ActiveRecord的模型和集合查找最干净的方法是什么? - What's the cleanest way to override ActiveRecord's find for both models and collections? 创建/删除项目的正确方法是什么? - Project creation / deletion, what's the proper way? 跟踪 ActiveRecord 的 i18n 验证消息的最佳方式是什么? - What's the best way to keep track of ActiveRecord's i18n validation messages? 覆盖Rails ActiveRecord销毁行为的最佳方法是什么? - What is the best way to override Rails ActiveRecord destroy behavior?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM