3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

#Rails で レコードが更新・保存されたかどうかを確認する ( saved_changes? )

Last updated at Posted at 2020-01-27
# Model Instance
user = User.first

user.saved_changes?
# => false

user.update!(name: "Alice")
# User Update (0.8ms)  UPDATE `users` SET `name` = 'Alice', `updated_at` = '2020-01-26 05:35:18' WHERE `users`.`id` = 1

user.saved_changes?
# => true

user.reload
user.saved_changes?
# => false

# When nothing commit on DB
user.update!(name: "Alice")
# (0.6ms)  BEGIN
# (0.5ms)  COMMIT

user.saved_changes?
# => false
# Then saved_changess?

user.update!(name: "Bob")
user.saved_changes?
# => true


user.reload
user.saved_changes?
# => false

user.name = "Carol"
user.saved_changes?
# => false

user.has_changes_to_save?
# => true

user.save!
user.saved_changes?
# => true

Ref

Rails 5.1 で attribute_was, attribute_change, attribute_changed?, changed?, changed 等が DEPRECATION WARNING - Qiita
https://qiita.com/htz/items/56798d53ec5988733fc6

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?