有一种方法来处理“after_save”和“after_destroy”“equal”? [英] There is a way to handle `after_save` and `after_destroy` "equally"?

查看:135
本文介绍了有一种方法来处理“after_save”和“after_destroy”“equal”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Rails 3.1.0,我想知道是否可以处理 after_save after_destroy 回调平等。也就是说,我需要为 after_save after_destroy 回调运行相同的方法。

I am using Rails 3.1.0 and I would like to know if it is possible to handle after_save and after_destroy callbacks "equally". That is, I need to run same methods for both after_save and after_destroy callbacks.

此时,我必须分别处理这些回调,即使这些回调完成相同的事情:

At this time I must handle those callbacks separately even if those accomplish to the same thing:

after_save do |record|
  # Make a thing
end

after_destroy do |record|
  # Make the same thing as in the 'after_save' callback
end

因此,有一种方法来处理 after_save after_destroy equal? p>

So, there is a way to handle after_save and after_destroy "equally"?

推荐答案

而不是块给予 after_save after_destroy 您的模型的方法名称作为符号。

Instead of a block give after_save and after_destroy a method name of your model as a symbol.

class ModelName < AR
  after_save :same_callback_method
  after_destroy :same_callback_method

  def same_callback_method
    # do the same for both callbacks
  end
end

这篇关于有一种方法来处理“after_save”和“after_destroy”“equal”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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