reload the record and clears changed attributes.
変更された属性をクリアして、もともとのレコードをリロードする。
> user.name => "kota" > user.name = 'hogehoge' => "hogehoge" > user.reload User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] => #<User id: 1, name: "kota", created_at: "2020-11-21 08:09:04", updated_at: "2020-11-21 08:09:04"> irb(main):008:0> user.name => "kota"
user.name
にhogehogeを代入しているが、reload
を実行するともとのkotaにもどっている。SQLを見ても特別なことはしていない。where
でuseridを検索しているだけだ。