Nim*_*deh 34 activerecord ruby-on-rails callback
我现在已经经历了一段时间的rails源代码,我认为除了以下方法之外还有更好的方法来获取所有回调列表:ActiveRecord::Callbacks::CALLBACKS
- 这是一个常量列表.
这意味着如果你使用像devise_invitable这样的宝石,它会添加一个:invitation_accepted
用分数调用的新回调:after
,:before
然后ActiveRecord::Callbacks::CALLBACKS
就无法工作了.
除了打开rails模块并确保每个模型类的内部回调列表之外,您知道一个简单的修复方法吗?
Sam*_*roa 45
您可以调用Model._save_callbacks
以获取保存时所有回调的列表.然后,您可以将其过滤到您需要的类型,例如:before
或者:after
像这样:
Model._save_callbacks.select {|cb| cb.kind == :before}
Run Code Online (Sandbox Code Playgroud)
同样适用于Model._destroy_callbacks
等
GMA*_*GMA 19
该文件说:"总共有19回调" ......但他们似乎不知道说什么所有的19实际上是!
对于那些像我一样使用Google搜索"所有ActiveRecord回调列表"的人,这里是列表(通过ActiveRecord::Callbacks::CALLBACKS
问题中描述的使用找到):
:after_initialize
:after_find
:after_touch
:before_validation
:after_validation
:before_save
:around_save
:after_save
:before_create
:around_create
:after_create
:before_update
:around_update
:after_update
:before_destroy
:around_destroy
:after_destroy
:after_commit
:after_rollback
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13313 次 |
最近记录: |