Skip to content

Instantly share code, notes, and snippets.

@chrisjpowers
Created July 6, 2011 21:42
Show Gist options
  • Save chrisjpowers/1068421 to your computer and use it in GitHub Desktop.
Save chrisjpowers/1068421 to your computer and use it in GitHub Desktop.
Saving without callbacks - any better way?
module SavingWithoutCallbacks
def save_without_callbacks
raise "Only works for saved records" if new_record?
updates = {}
changes.each {|key, tuple| updates[key] = tuple.last}
self.class.update_all(updates, {:id => self.id}, {:limit => 1})
end
end
ActiveRecord::Base.send :include, SavingWithoutCallbacks
@ryanbriones
Copy link

Turns out that ActiveRecord::Base objects already have a method called save_without_callbacks via an alias_method_chain on save/create/update/etc that's put there for adding callbacks to persistence methods (https://github.com/rails/rails/blob/v2.3.11/activerecord/lib/active_record/callbacks.rb#L223-225). Thanks to Bryce Kerley (@BonzoESC) for the answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment