Rails: model.save returns false, but models.errors is an empty hash

I have a model object where .save returns false. Subsequently, it has a .errors property, which is an empty hash. Shouldn't the hash contain a list of errors? How else can I determine why the save isn't working?

TY, Fred

+3


source to share


2 answers


This means that one of your callbacks will probably stop saving, but does not contain a validation error.

Check return values, in particular any callbacks, before_

and make sure they don't returnfalse



If they return false

, then the active entry will stop future callbacks and return false from save.

You can read a bit about it here under "Canceling Callbacks"

+10


source


1) Disable before_create, before_save, before_update and check where it saves the day

2) If rollback is caused by one of these methods, make sure that these methods return true if you do not plan to rollback.

For example, if you set the default for a boolean field to avoid nil, you would probably do it like this.



def set_defaults_before_create
  self.my_boolean_field ||= false
end

      

In this example, the set_defaults_before_create method always returns false and thus rolls back the transaction. So refactor it to return true

def set_defaults_before_create
  self.my_boolean_field ||= false
  true
end

      

+1


source







All Articles