如何以编程方式在 Rails 中运行 before_destroy 和其他活动记录挂钩
我想覆盖rails中的destroy和destroy_all以及delete和delete_all方法来完成软删除(将活动位设置为false),而不使用任何插件。我想在适当的情况下在这些操作中运行 before_destroy 过滤器。那么我如何从 Rails 代码以编程方式运行 before_destroy 挂钩和其他此类挂钩?作为一个附带问题,当尝试对 activerecord 方法(如 destroy、destroy_all 等)进行 Monkeypatch 时,我还应该注意哪些其他细节?
I wan to override the destroy and the destroy_all, and the delete and the delete_all methods in rails to accomplish soft deletion (set active bit to false), without using any plugin for that. I want to run the before_destroy filter in these actions where appropriate. So how can i run the before_destroy hook, and other such hooks, programmatically from rails code? As a side question, what other detail should i be aware of when trying to monkeypatch an activerecord method like destroy, destroy_all etc?
我想你想遵循困难的方式:)也许像 https://github.com/JackDanger/permanent_records< 这样的宝石< /a> 可以帮助您解决问题。
I think you want to follow the hard way :) Maybe a gem like https://github.com/JackDanger/permanent_records could help you to solve your problem.