开发者

How do I use after_save_callback_chain?

开发者 https://www.devze.com 2023-02-18 01:41 出处:网络
The main documentation describe it at debugging callbacks but it seems to not exists: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

The main documentation describe it at debugging callbacks but it seems to not exists:

http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

I've tried to use just like the example and it returns me:

ruby-1.8.7-p330 :026 > Device.after_save_callback_chain
NoMethodError: undefined method `after_save_callback_chain' for #<Class:0x104bc1060>
   开发者_开发技巧 from /rvm/gems/ruby-1.8.7-p330/gems/activerecord-3.0.5/lib/active_record/base.rb:1008:in `method_missing'
    from (irb):26


Seems like this method has disappeared in Rails 3. I've used the following before :

Model._save_callbacks.select { |callback| callback.kind.eql?(:after) }

This will get you any after_save callbacks. You can then get further information like the proc that's being called by checking the .filter attribute :

Model._save_callbacks.select { |callback| callback.kind.eql?(:after) }.collect(&:filter)

Hopefully someone has a nicer answer than this though.

This _#{kind}_callbacks method is defined on ActiveSupport I think so you can do similar stuff with controllers and whatnot I'd assume.

0

精彩评论

暂无评论...
验证码 换一张
取 消