113

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?

3
  • 7
    I 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. 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?
    – kdt
    Jan 17, 2011 at 15:25
  • It was code inspection, and doing diffs against version control. Jan 17, 2011 at 19:33

6 Answers 6

147

If @user.save (for example) returns false, then just run this to get all the errors:

@user.errors.full_messages
2
  • 15
    As 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)
    – kdt
    Jan 17, 2011 at 15:11
  • The sentence returns an Array (not String) as of Rails 6.1. For your information. Jun 18, 2021 at 0:51
126

Try using the bang version save! (with an exclamation mark at the end) and inspecting the resulting error.

9
  • 6
    save! 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?
    – kdt
    Jan 17, 2011 at 14:37
  • 1
    If 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. Jan 17, 2011 at 15:02
  • 1
    I 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.
    – pduey
    May 10, 2011 at 23:30
  • 3
    FYI, calling save! can raise ActiveRecord::RecordInvalid (since it runs validations) or ActiveRecord::RecordNotSaved so that's what you'll want to rescue.
    – Dennis
    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. Jun 6, 2018 at 19:28
51

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.

6
  • 2
    As per Jeff's comment, the problem turned out to be a before_save callback returning false.
    – kdt
    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. ;) Feb 18, 2013 at 15:15
  • 2
    A nice way to ensure a true return value is true.tap { do_something } 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!
    – andy
    May 16, 2013 at 0:46
  • 1
    @andy guides.rubyonrails.org/…
    – Andrew
    May 16, 2013 at 1:09
3

Yea, I fixed this issue by making sure I return true in all my before_* callbacks then it starts working :)

0

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
-2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.