Random Ramblings

There are a few times, not many, when you really don't want to trigger AR callbacks on a save. Take the following simple example:

class Article < ActiveRecord::Base
  include ActionView::Helpers::SanitizeHelper
  before_save :sanitize_body_text

  def sanitize_body_text
     self.body_text = sanitize(self.body_text)
  end
end

article.update_attribute(:posted_at, Time.now - 100)

You probably don't want to fire up an expensive method like sanitize when updating the posted_at time.

save_without_callbacks solves that problem.

To Install

./script/plugin install https://tangofoxtrot.com/svn/plugins/save_without_callbacks

On any AR.object that you want to skip the callbacks for:

@something.skip_callbacks = true

*note you cannot set this on mass assignment*

Then call:

@something.save

This plugin can be potentially dangerous! Make sure this is the behavior you want.

For example acts_as_list:

before_create  :add_to_list_bottom
before_destroy :remove_from_list

def remove_from_list
  if in_list?
    decrement_positions_on_lower_items
    update_attribute position_column, nil
  end
end

If you set skip_callbacks to true and updated the position or created/destroyed an object the positions will be off.

This is best used in a situation where you are updating some arbitrary, non relationship, data.

Leave a Reply