I have an ActiveRecord model which is returning true
from valid?
(and .errors is empty), but is returning false
from save()
. If the model instance is valid, how can I find out what's causing the save to fail?
-
7I had this problem a couple of weeks ago. Some refactoring had left a before_save function returning false all the time, which causes save to fail.– Jeff PaquetteCommented Jan 17, 2011 at 14:10
-
1@Jeff -- thanks, it turns out that there was a :before_save method returning false. How did you find out? Was it just code inspection?– kdtCommented Jan 17, 2011 at 15:25
-
It was code inspection, and doing diffs against version control.– Jeff PaquetteCommented Jan 17, 2011 at 19:33
7 Answers
If @user.save
(for example) returns false
, then just run this to get all the errors:
@user.errors.full_messages
-
15As I mentioned in the question, .valid? is true -- ie there are no validation errors. I've checked that .errors is returning an empty list as well (I've updated the question to point that out)– kdtCommented Jan 17, 2011 at 15:11
-
The sentence returns an Array (not String) as of Rails 6.1. For your information. Commented Jun 18, 2021 at 0:51
Try using the bang version save!
(with an exclamation mark at the end) and inspecting the resulting error.
-
6save! is just throwing a RecordNotSaved (when I print the .message of the exception I just get the name of the exception class). Is there somewhere I should be looking for more detail?– kdtCommented Jan 17, 2011 at 14:37
-
1If you're in Rails development mode, it should print a full description of the error with stack trace. Take a look there for any clues and/or post it here. Commented Jan 17, 2011 at 15:02
-
1I use the console, load the object (e.g., o = Object.find #id), then do o.save! like the answer says. It prints out why it's not saving.– pdueyCommented May 10, 2011 at 23:30
-
3FYI, calling
save!
can raiseActiveRecord::RecordInvalid
(since it runs validations) orActiveRecord::RecordNotSaved
so that's what you'll want to rescue.– DennisCommented Nov 8, 2014 at 21:03 -
3+1 because this is the least unsatisfying answer to the fundamental question of how to diagnose
.save
failures that are not due to validation. The "least unsatisfying" qualification refers to Rails, not this answer. Commented Jun 6, 2018 at 19:28
Check all your callbacks.
I had a problem like this where I had and "after_validate" method that was failing after I had made a bunch of changes to the model. The model was valid but the "after_validate" was returning false, so if I used model.valid
it said true, but then if I saved it gave me validation errors (passed through from the after_validate callback). It was weird.
Look at the application trace and you should be able to see what line of code is raising the exception.
-
2As per Jeff's comment, the problem turned out to be a before_save callback returning false.– kdtCommented Jan 17, 2011 at 15:25
-
3@kdt - that's exactly what my problem was. I hadn't thought about it because the before_save was just meant to set a property, but because it was setting it to a false value, that was implicitly returned and that made the save fail silently. On the bright side, I now have the option of fixing this code by adding the line
"Hey! That's MY fake leg!" # Believe it or not, this is important
. Not that I would do that. ;) Commented Feb 18, 2013 at 15:15 -
2A nice way to ensure a true return value is
true.tap { do_something }
Commented Feb 18, 2013 at 16:14 -
wow, what an obscure issue. Would never have guesses that a callback returning false would have stopped saving. Could someone point me to the docs on this? Thanks for pointing this out!– andyCommented May 16, 2013 at 0:46
-
1
Yea, I fixed this issue by making sure I return true in all my before_* callbacks then it starts working :)
In active record, when you call save(), it returns a boolean, true or false
Example:
user = User.new(user_params)
if user.save
# do some stuff, redirect to users_path
else
render :new
end
You also have an option of checking if it is valid by doing user.valid? If you want to find errors, you need to use the create method, which returns an object and the object will have errors array populated if any.
u = User.create()
u.errors
I hope this helps.
Make sure you aren't trying to save a deleted record.
I had the same issue. But unlike the selected answer - my issue wasn't callbacks related.
In my case I had tried to save to a deleted record (deleted from DB).
@user = User.new
@user.save! # user saved to DB
@user.persisted? # true
@user.destroy # user deleted from DB
@user.persisted? # false, user still has its id
@user.valid? # return true
@user.errors # empty
@user.save # return false
@user.save! # raise ActiveRecord::RecordNotSaved
The problem I had was that I had forgotten to add the validation to the model.
class ContactGroup < ActiveRecord::Base
validates_presence_of :name
end