Active Record Attribute Methods Dirty¶ ↑
提供了一种跟踪 Active Record 模型中更改的方法。它添加了来自 ActiveModel::Dirty 的所有方法,并添加了特定于数据库的方法。
新创建的 Person 对象未更改
class Person < ActiveRecord::Base end person = Person.create(name: "Allison") person.changed? # => false
更改姓名
person.name = 'Alice' person.name_in_database # => "Allison" person.will_save_change_to_name? # => true person.name_change_to_be_saved # => ["Allison", "Alice"] person.changes_to_save # => {"name"=>["Allison", "Alice"]}
保存更改
person.save person.name_in_database # => "Alice" person.saved_change_to_name? # => true person.saved_change_to_name # => ["Allison", "Alice"] person.name_before_last_save # => "Allison"
与 ActiveModel::Dirty 类似,方法可以作为 saved_change_to_name? 调用,或者通过将参数传递给通用方法 saved_change_to_attribute?("name") 来调用。
- A
- C
- H
- R
- S
- W
实例公共方法
attribute_before_last_save(attr_name) 链接
返回上次保存之前属性的原始值。
此方法在 after 回调中非常有用,可以获取触发回调执行的保存之前的属性的原始值。它可以被调用为 name_before_last_save,而不是 attribute_before_last_save("name")。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 108 def attribute_before_last_save(attr_name) mutations_before_last_save.original_value(attr_name.to_s) end
attribute_change_to_be_saved(attr_name) 链接
返回将在下一次保存时持久化的属性的更改。
此方法在验证和 before 回调中非常有用,用于查看记录保存时将发生的属性的更改。它可以被调用为 name_change_to_be_saved,而不是 attribute_change_to_be_saved("name")。
如果属性会更改,结果将是一个包含原始值和即将保存的新值的数组。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 152 def attribute_change_to_be_saved(attr_name) mutations_from_database.change_to_attribute(attr_name.to_s) end
attribute_in_database(attr_name) 链接
返回数据库中属性的值,而不是将在下一次保存记录时持久化的内存中的值。
此方法在验证和 before 回调中非常有用,用于查看在保存即将进行的更改之前的属性的原始值。它可以被调用为 name_in_database,而不是 attribute_in_database("name")。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 164 def attribute_in_database(attr_name) mutations_from_database.original_value(attr_name.to_s) end
attributes_in_database() 链接
返回将在下次保存记录时更改的属性的哈希。
哈希键是属性名,哈希值是数据库中的原始属性值(而不是即将保存的内存中的值)。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 191 def attributes_in_database mutations_from_database.changed_values end
changed_attribute_names_to_save() 链接
返回将在下次保存记录时更改的任何属性名的数组。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 181 def changed_attribute_names_to_save mutations_from_database.changed_attribute_names end
changes_to_save() 链接
返回一个包含将在下次保存时持久化的所有更改的哈希。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 175 def changes_to_save mutations_from_database.changes end
has_changes_to_save?() 链接
下次调用 save 时是否会有要持久化的更改?
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 169 def has_changes_to_save? mutations_from_database.any_changes? end
reload(*) 链接
reload 记录并清除已更改的属性。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 63 def reload(*) super.tap do @mutations_before_last_save = nil @mutations_from_database = nil end end
saved_change_to_attribute(attr_name) 链接
返回上次保存期间属性的更改。如果属性已更改,结果将是一个包含原始值和已保存值(值)的数组。
此方法在 after 回调中非常有用,用于查看触发回调执行的保存期间属性的更改。它可以被调用为 saved_change_to_name,而不是 saved_change_to_attribute("name")。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 98 def saved_change_to_attribute(attr_name) mutations_before_last_save.change_to_attribute(attr_name.to_s) end
saved_change_to_attribute?(attr_name, **options) 链接
此属性在上次保存时更改了吗?
此方法在 after 回调中非常有用,用于确定属性在触发回调执行的保存期间是否已更改。它可以被调用为 saved_change_to_name?,而不是 saved_change_to_attribute?("name")。
选项¶ ↑
from-
指定后,此方法将返回 false,除非原始值等于给定值。
到-
指定后,此方法将返回 false,除非值将更改为给定值。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 86 def saved_change_to_attribute?(attr_name, **options) mutations_before_last_save.changed?(attr_name.to_s, **options) end
saved_changes() 链接
返回一个包含刚保存的所有更改的哈希。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 118 def saved_changes mutations_before_last_save.changes end
saved_changes?() 链接
上次调用 save 时是否有要更改的更改?
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 113 def saved_changes? mutations_before_last_save.any_changes? end
will_save_change_to_attribute?(attr_name, **options) 链接
下次我们保存时,此属性会更改吗?
此方法在验证和 before 回调中非常有用,用于确定下次调用 save 是否会更改特定属性。它可以被调用为 will_save_change_to_name?,而不是 will_save_change_to_attribute?("name")。
选项¶ ↑
from-
指定后,此方法将返回 false,除非原始值等于给定值。
到-
指定后,此方法将返回 false,除非值将更改为给定值。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 138 def will_save_change_to_attribute?(attr_name, **options) mutations_from_database.changed?(attr_name.to_s, **options) end