class

ActiveRecord::RecordNotSaved

v3.1.0 - Show latest stable - Superclass: ActiveRecordError

Raised by ActiveRecord::Base.save! and ActiveRecord::Base.create! methods when record cannot be saved because record is invalid.

Files

  • activerecord/lib/active_record/errors.rb

1Note

ActiveRecord::RecordNotSaved can be triggered by accidental false return values in callbacks

tadman ยท Jun 3, 20099 thanks

You may have this exception raised if any of the defined callbacks such as ActiveRecord::Base#before_save or ActiveRecord::Base#before_create return false.

This can happen accidentally. For example:

class MyModel < ActiveRecord::Base
before_save :assign_default_foo

protected
def assign_default_foo
  self.foo = false
end
end

Since assign_default_foo leaves a false value on the stack, the model will not be saved. A way around this is to simply leave nil or an empty return instead:

class MyModel < ActiveRecord::Base
before_save :assign_default_foo

protected
def assign_default_foo
  self.foo = false
  nil
end
end