Dealing with ActiveRecord::RecordNotSaved

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 Over 13 years ago