How can I avoid running ActiveRecord callbacks?

How can I avoid running ActiveRecord callbacks?
因为rails里的callback是用method_alias_chains实现的,所以可以这样.
p = Person.new(:name => 'foo')
p.send(:create_without_callbacks)

p = Person.find(1)
p.send(:update_without_callbacks)


class MyModel < ActiveRecord::Base
  attr_accessor :skip_after_save

  def after_save
    return false if @skip_after_save
    ... blah blah ...
  end
end

...

m = MyModel.new # ... etc etc
m.skip_after_save = true
m.save

http://stackoverflow.com/questions/632742/how-can-i-avoid-running-activerecord-callbacks

关于send(:update_without_callbacks)有时候会报错:
你可以这样写,在某个model里,self.__send__(:update_without_callbacks),
可以这么写a=A.find(1);a.__send__(:update_without_callbacks),
但是这样写: a.b.__send__(:update_without_callbacks)会出错

你可能感兴趣的:(Ruby,ActiveRecord,Rails)