Save ActiveRecord models without callbacks or validations (in Rails 2 and Rails 3)

Updated . Posted . Visible to the public.

Rails 2

You can use

record.send(:update_without_callbacks)

or

record.send(:create_without_callbacks)

This can be used as a lightweight alternative to machinist's make or FactoryGirl's create, when you just need objects in the database but don't care about any callbacks or validations. Note that create_without_callbacks does not return the object, so you might want to do

record = Record.new.tap(&:create_without_callbacks)

Rails 3

Rails 3 no longer comes with update_without_callbacks or create_without_callbacks. There is no such thing in vanilla Rails 3.

There is a gem sneaky-save Show archive.org snapshot that restores the missing functionality with a #sneaky_save method.

Tobias Kraze
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Tobias Kraze to makandra dev (2010-09-17 16:32)