LoginSignup
14
10

More than 5 years have passed since last update.

rails consoleでupdateができない理由を知りたい時(rollbackされる)

Posted at

以下を使って、エラーの内容を見ましょう。

インスタンス.errors.full_messages
(例)user.errors.full_messages

起きたこと

rails console上で名前だけを更新しようとして更新できなかった。


更新がrollback(巻き戻し)される
>> user.update(name: "Kohei Kishimoto")
   (0.1ms)  begin transaction
  User Exists (0.3ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) AND "users"."id" != ? LIMIT ?  [["email", "example@rai
lstutorial.org"], ["id", 1], ["LIMIT", 1]]
   (0.1ms)  rollback transaction
=> false

エラーメッセージを確認
パスワード入れろやコラ、と言われている
>> user.errors.full_messages
=> ["Password can't be blank", "Password is too short (minimum is 6 characters)"]


パスワード入れて更新する
>> user.update(name: "koheikishimoto", password: "1234567890", password_confirmation: "1234567890")
   (0.1ms)  begin transaction
  User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) AND "users"."id" != ? LIMIT ?  [["email", "example@rai
lstutorial.org"], ["id", 1], ["LIMIT", 1]]
  User Update (1.9ms)  UPDATE "users" SET "name" = ?, "updated_at" = ?, "password_digest" = ? WHERE "users"."id" = ?  [["name", "koheikishimoto"],
["updated_at", "2018-05-08 20:08:42.795109"], ["password_digest", "$2a$10$wD0rVfl8RiRUx2EfXMc6n.cfD/gTm/rbo.n6zxxZkdd/Y9rqEe5/G"], ["id", 1]]
   (1.6ms)  commit transaction

更新できた。
=> true
14
10
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
14
10