开发者

Callback - after_save but NOT create

How can you separate callbacks so that after_create runs one set of code, but !after_create, so to speak, runs another开发者_如何转开发?


after_create callback for new object, after_update for persisted one.


after_create After new object created

after_update After existing object updated

after_save Both creation and update


You can have multiple callbacks which only execute based on conditions

model.rb

after_create :only_do_if_this
after_create :only_do_if_that

def only_do_if_this
  if do_this?
    # code...
  end
end

def only_do_if_that
  if do_that?
    # code...
  end
end

You can also add the condition to the callback itself

after_create :only_do_if_this, :if => proc { |m| m.do_this? }
after_create :only_do_if_that, :if => proc { |m| m.do_that? }

def only_do_if_this
  # code ...
end

def only_do_if_that
  # code...
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜