How to debug silent database errors (for example, "save") in Rails?

How to get additional information about errors that occur in Rails without changes, for example @ object.save?

+4
source share
3 answers

According to this post , something like the following works:

logger.debug @item.errors.full_messages 
+5
source

Add an error so that the error occurs when the validation fails.

 @article.save! # ActiveRecord::RecordInvalid: Validation failed: Title can't be blank... 

Always use this method instead of save unless you expect verification confirmation.

+7
source

Sometimes AR silently fails for reasons other than validation errors. A couple more things to check:

  • AR callbacks that do not return true (e.g. before_save)
  • Invalid Parent / Child Records
  • @ item.valid ?, @ item.errors.full_messages
  • @ item.changes, @ item.changed?

I also included user456584 with thanks for checking validation errors. And, as Semyon said, @ item.save! at least throw an exception, even if it is not particularly useful.

+4
source

Source: https://habr.com/ru/post/1438767/


All Articles