跳至內容 跳至搜尋

Active Record 屬性方法變更

提供追蹤 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)

傳回上次儲存前屬性的原始值。

此方法適用於執行逾後 callback,以取得觸發 callback 執行的儲存前屬性原始值。它可以用 name_before_last_save 代替 attribute_before_last_save("name") 呼叫。

# 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)

傳回下次儲存時會持續處理的屬性變更。

此方法適用於驗證和執行前 callback,以了解記錄儲存時屬性變更。它可以用 name_change_to_be_saved 代替 attribute_change_to_be_saved("name") 呼叫。

如果屬性會變更,結果會是一個陣列,包含準備儲存的原始值和新值。

# 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)

傳回資料庫中屬性的值,相對於記憶中的值會在下一次儲存記錄時持續處理。

此方法適用於驗證和執行前 callback,以了解任何準備儲存變更前的屬性原始值。它可以用 name_in_database 代替 attribute_in_database("name") 呼叫。

# 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()

傳回一組雜湊,裡面有下次儲存的記錄時將會變更的屬性。

雜湊金鑰為屬性名稱,雜湊值為資料庫中的原始屬性值(相對於即將儲存的記憶體值)。

# 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()

傳回一個陣列,裡面有下次儲存的記錄時將會變更的屬性名稱。

# 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()

傳回一組雜湊,包含下次儲存時將會持續存在的變更。

# 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 是否將會有變更要持續化?

# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 169
def has_changes_to_save?
  mutations_from_database.any_changes?
end

reload(*)

重新載入記錄並清除已變更的屬性。

# 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")

# 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。

# 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()

回傳包含剛才儲存的所有變更之雜湊。

# File activerecord/lib/active_record/attribute_methods/dirty.rb, line 118
def saved_changes
  mutations_before_last_save.changes
end

saved_changes?()

上一次呼叫 save 是否有任何變更需要變更?

# 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。

# 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