📜  rails activerecord saved_change_to - Ruby (1)

📅  最后修改于: 2023-12-03 15:19:41.643000             🧑  作者: Mango

Rails ActiveRecord saved_change_to

In the Rails ActiveRecord framework, saved_change_to is a method that allows you to determine if an attribute has been changed and saved to the database.

Syntax
object.saved_change_to_attribute?
Return Values
  • If the attribute has been changed and saved to the database, true will be returned.
  • If the attribute has not been changed or has been changed but not saved to the database, false will be returned.
Examples
Example 1:
@user = User.first
@user.name = "John Doe"
@user.saved_change_to_name? #=> true

In this example, the @user.name attribute has been changed and saved to the database. Therefore, true is returned from saved_change_to_name?.

Example 2:
@user = User.first
@user.name = "John Doe"
@user.saved_change_to_name? #=> true

@user.save
@user.saved_change_to_name? #=> false

In this example, the @user.name attribute has been changed and saved to the database. Therefore, true is returned from saved_change_to_name?.

After saving the @user object, the saved_change_to_name? method returns false because the attribute has been saved to the database.

References