Dealing with ActiveRecord::RecordNotSaved

Posted Over 13 years ago. Visible to the public.

If you get an ActiveRecord::RecordNotSaved error, a method inside one of your model's callback chains (before_save etc) possibly returned false.

This commonly happens when you have a method setting attributes and the last one is a boolean set to false (as the value of the last statement is returned). Fix this by simply calling true at the end of such methods:

def hide
  self.visible = false
  true
end

Note that nil won't cause this behavior. Thus, you can use an if without problems -- if you are not returning false inside.

Arne Hartherz
Last edit
Over 9 years ago
Arne Hartherz
Keywords
exception, before_save, before_create, before_update
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2010-10-11 16:47)