Search code examples
ruby-on-railsrubyactiverecordsavebefore-filter

ignore before_update when save


some case I don't want execute before_update. please help me.

case A: in case I want used before_update

obj = Object.find(id)
obj.save

but case B I don't want used before_update

obj = Object.find(id)
obj.save # in case I want used before_update

Solution

  • update_without_callbacks and create_without_callbacks are private methods. These methods will not call any callbacks.

    obj = Object.find(id)
    obj.send(:update_without_callbacks)
    
    obj = Object.new(:name => 'foo')
    obj.send(:create_without_callbacks)