ActiveRecord回调列表 [英] ActiveRecord Callbacks List

查看:176
本文介绍了ActiveRecord回调列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览rails源一段时间,我不认为有更好的方法获取所有回调的列表除了: ActiveRecord :: Callbacks :: CALLBACKS - 这是一个常数列表。

I've been going through the rails source for a while now, and I don't think there's a better way of getting the list of all callbacks other than: ActiveRecord::Callbacks::CALLBACKS – which is a constant list.

意味着如果你使用像devise_invitable这样的gem,回调称为:invitation_accepted ,分数:之前然后 ActiveRecord :: Callbacks :: CALLBACKS 将无法工作。

Meaning if you're using a gem like devise_invitable that adds a new callback called :invitation_accepted with the score :after and :before then ActiveRecord::Callbacks::CALLBACKS will not work.

你知道一个简单的修复,其他

Do you know of an easy fix, other than opening up rails modules and making sure there's an internal list of call-backs per model class?

推荐答案

您可以调用 Model._save_callbacks 以获取保存时的所有回调的列表。
然后你可以过滤它到什么你需要什么。 :之前:之后如下:

You can call Model._save_callbacks to get a list of all callbacks on save. You can then filter it down to what kind you need e.g. :before or :after like this:

Model._save_callbacks.select {|cb| cb.kind == :before}

使用 Model._destroy_callbacks 等。

这篇关于ActiveRecord回调列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆