Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get detailed error messages from rails console?

Say I wish to create a user from console with an email which already exists. If I do User.create(...)

Because of model validations (uniqueness in this case). create fails and the output in rails console will say: rollback transaction (user is not saved)

For the purpose of testing. Is their a way I can get the explicit error which triggered the rollback? In this case it would say something like: "ROLLBACK: email is not unique".

like image 372
MrPizzaFace Avatar asked Jan 01 '14 07:01

MrPizzaFace


2 Answers

You can also do:

User.create!

The 'bang' will force it to show you the errors without additional steps.

like image 76
simonmorley Avatar answered Oct 01 '22 09:10

simonmorley


You could do the following in Rails console:

>> user = User.new(...)
>> user.save
>> user.errors.messages

That way, you know what errors caused save to fail.

like image 31
Gjaldon Avatar answered Oct 01 '22 11:10

Gjaldon