Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In rails, how can I find out what caused a .save() to fail, other than validation errors?

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?

like image 974
kdt Avatar asked Jan 17 '11 14:01

kdt


People also ask

Can we save an object in the DB if its validations do not pass?

Validations are typically run before these commands are sent to the database. If any validations fail, the object will be marked as invalid and Active Record will not perform the INSERT or UPDATE operation. This helps to avoid storing an invalid object in the database.

What happens on Save rails?

The purpose of this distinction is that with save! , you are able to catch errors in your controller using the standard ruby facilities for doing so, while save enables you to do the same using standard if-clauses.

What is Active Record in Ruby on Rails?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.


1 Answers

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

@user.errors.full_messages 
like image 152
Sam Alex Avatar answered Sep 29 '22 19:09

Sam Alex