skipping after_update callback

class MyModel < ActiveRecord::Base
  after_update :do_something
  attr_accessor :should_do_something

  def should_do_something?
    should_do_something != false
  end

  def do_something
    if should_do_something?
      ...
    end
  end
end


y = MyModel.new
y.save! # callback is triggered

n = MyModel.new
n.should_do_something = false
n.save! # callback isn't triggered

=====================
api
update_attribute(name, value) 不触发验证
Updates a single attribute and saves the record without going through the normal validation procedure. This is especially useful for boolean flags on existing records. The regular update_attribute method in Base is replaced with this when the validations module is mixed in, which it is by default.

[ hide source ]
      # File vendor/rails/activerecord/lib/active_record/base.rb, line 2614
2614:       def update_attribute(name, value)
2615:         send(name.to_s + '=', value)
2616:         save(false)
2617:       end



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