Created
July 6, 2011 21:42
-
-
Save chrisjpowers/1068421 to your computer and use it in GitHub Desktop.
Saving without callbacks - any better way?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.