class
ActiveRecord::RecordNotSaved
v7.1.3.2 -
Show latest stable
- Superclass: ActiveRecordError
Raised by {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!] and {ActiveRecord::Base.create!}[rdoc-ref:Persistence::ClassMethods#create!] methods when a record is invalid and cannot be saved.
Attributes
| [R] | record |
Files
- activerecord/lib/active_record/errors.rb
1Note
ActiveRecord::RecordNotSaved can be triggered by accidental false return values in callbacks
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