Ruby on rails 回调-保存但不创建后
如何分离回调,以便在创建后Ruby on rails 回调-保存但不创建后,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,如何分离回调,以便在创建后运行一组代码,但是!可以说,在创建之后,在为新对象创建回调之后运行另一个?,在为持久对象更新之后运行另一个回调。您可以有多个回调,这些回调只根据条件执行 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_tha
运行一组代码,但是!可以说,在创建之后,在为新对象创建回调之后运行另一个?,在为持久对象更新之后运行另一个回调。您可以有多个回调,这些回调只根据条件执行
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
您还可以将条件添加到回调本身
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
创建后
创建新对象后
更新后的
更新现有对象后
保存后
创建和更新您到底想实现什么?据我所知,!“创建后”
不是回调。看这里@Jatin-这就是我的观点。我想知道如何实现只在更新记录而不创建记录时才触发的回调。这是否回答了上述问题?若有,请教我怎么做。@Jatin,因为我理解这个问题,是的。但我同意你的说法,问题的措辞有点混乱。是的,问题不够清楚,我们可以无缘无故地争论。谢谢,这正是我一直在寻找的。有没有一种方法可以将这两个条件放在一个回调行上,并且在其中一个条件满足时仍然执行回调,或者条件必须分开?