LoginSignup
1
1

More than 3 years have passed since last update.

Rails6 DBにsaveする時にROLLBACKしてしまい原因がわからない時

Posted at

目的

  • DBにsaveする際にROLLBACKしているときのエラーの表示方法をまとめる

実施方法

  • saveの命令の後ろに!をつけてあげるとエラー文が表示される。

具体的な例

  • @user.saveをただ実行しただけの時

    User Exists? (0.5ms)  SELECT 1 AS one FROM `users` WHERE `users`.`email` = 
    BINARY 'miriwo.rails@gmail.com' LIMIT 1
    (0.2ms)  ROLLBACK
    => false
    
  • @user.save!を実行した時

    User Exists? (0.5ms)  SELECT 1 AS one FROM `users` WHERE `users`.`email` = 
    BINARY 'miriwo.rails@gmail.com' LIMIT 1
    (0.2ms)  ROLLBACK
    Traceback (most recent call last):
        1: from (irb):21
    ActiveRecord::RecordInvalid (Validation failed: Password can't be blank)
    
  • 最後にエラー文が一文追加されたことがわかる

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1