Skip to content

6-3演習  emailでuserオブジェクトを検索し取得後、データの変更をしようとしたが、できない!!なぜ? #9

@koya-kudaka

Description

@koya-kudaka

・状態
6-3-4「ユーザーの認証と作成」の演習をやっている。
ユーザーを検索して、user変数に渡した後、user.nameの値を変更しようとした。ところが、これが変更できない!
尚、実行はコマンドでDBに影響を与えないようにしている。

・今回の目的
なぜできないのか、メッセージを読み解きながら、理由を探してみる

・実際のコード

Kudaka-KouyanoMacBook-ea:sample_app kudaka$ rails console --sandboxRunning via Spring preloader in process 32783
Loading development environment in sandbox (Rails 6.0.3)
Any modifications you make will be rolled back on exit
irb(main):001:0> user = User.find_by(email: "michael@example.com")
   (0.2ms)  begin transaction
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."email" = ? LIMIT ?  [["email", "michael@example.com"], ["LIMIT", 1]]
=> #<User id: 1, name: "Michael Hartl", email: "michael@example.com", created_at: "2020-10-13 07:40:23", updated_at: "2020-10-13 07:40:23", password_digest: [FILTERED]>
irb(main):002:0> user.name
=> "Michael Hartl"
irb(main):003:0> user.name = "kkkk"
=> "kkkk"
irb(main):004:0> user.save
   (0.1ms)  SAVEPOINT active_record_1
  User Exists? (0.1ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = ? AND "users"."id" != ? LIMIT ?  [["email", "michael@example.com"], ["id", 1], ["LIMIT", 1]]
   (0.1ms)  ROLLBACK TO SAVEPOINT active_record_1
=> false

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions