Active Record 属性方法 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_change # => "Allison"
与 ActiveModel::Dirty
类似,可以将方法调用为 saved_change_to_name?
,或将参数传递给通用方法 saved_change_to_attribute?("name")
。
- A
- C
- H
- R
- S
- W
实例公共方法
attribute_before_last_save(attr_name) 链接
返回上次保存前某个属性的原始值。
此方法在回调之后很有用,用于获取触发回调运行的保存之前某个属性的原始值。可以将其调用为 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) 链接
返回将在下次保存期间保留的属性更改。
此方法在验证和回调之前很有用,用于查看在记录保存时将发生的属性更改。它可以调用为 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) 链接
返回数据库中的属性值,而不是下次保存记录时将保留的内存中值。
此方法在验证和回调之前很有用,用于查看在任何即将保存的更改之前属性的原始值。它可以调用为 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) 链接
返回上次保存期间对属性的更改。如果属性已更改,结果将是一个包含原始值和已保存值的数组。
此方法在回调之后很有用,可以查看在触发回调运行期间属性的更改。它可以调用为 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) 链接
我们上次保存时此属性是否更改?
此方法在回调之后很有用,可用于确定在触发回调运行时是否已更改某个属性。它可以作为 saved_change_to_name?
调用,而不是 saved_change_to_attribute?("name")
。
选项
from
-
如果指定了此选项,则此方法仅在原始值等于给定值时才返回 false。
to
-
如果指定了此选项,则此方法仅在值将更改为给定值时才返回 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) 链接
下次保存时,此属性是否会更改?
此方法在验证和回调之前很有用,可用于确定下次调用 save
是否会更改特定属性。它可以作为 will_save_change_to_name?
调用,而不是 will_save_change_to_attribute?("name")
。
选项
from
-
如果指定了此选项,则此方法仅在原始值等于给定值时才返回 false。
to
-
如果指定了此选项,则此方法仅在值将更改为给定值时才返回 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