English 中文(简体)
有一种办法处理“after后”和“after后”?
原标题:There is a way to handle `after_save` and `after_destroy` "equally"?

我正在使用铁路3.1.0,我很想知道,能否处理<条码>以下:save和<条码>仅次于_destroy的“平等”。 这就是说,在<0>和<代码>之后,我需要使用同样的方法。

此时,我必须单独处理这些反馈,即使这些反馈能够做到同样:

after_save do |record|
  # Make a thing
end

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

因此,there 是处理<条码>的一条途径,仅次于_save和<条码> 之后的“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_commit do |record|
  # Is called after creating, updating, and destroying.
end

http://apidock.com/rails/ActiveRecord/Transactions/ClassMethods/after_commit

class Foo < ActiveRecord::Base
  after_save :my_callback
  after_destroy :my_callback

  private
  def my_callback
    #Do stuff
  end
end




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签