#saved_changes當關系被破壞時,有沒有類似的東西會告訴我?破壞通過accepts_nested_attributes_for.
我有一個處理管道,我的物件在保存后會通過它,并且該管道需要知道關系是否被破壞。不幸的是,當我my_obj.images銷毀影像后呼叫時,#image是一個空陣列。
我還沒有找到“破壞前的影像”方法。我懷疑在提交更改之前我必須查看物件并在其上設定一個標志,表明某些東西正在被破壞。如果可能的話,我真的很想避免這種情況。
uj5u.com熱心網友回復:
我不止一次需要這個,所以我為它寫了一個 gem:
https://github.com/brandoncc/activerecord-nested_attribute_destruction
這是一個使用示例:
class Harbor
has_many :ships
accepts_nested_attributes_for :ships, allow_destroy: true
end
harbor.ships_attributes = [harbor.ships.first.attributes.merge('_destroy': true)]
harbor.save!
harbor.ships_destroyed_during_save? # true
看到最后一行代碼了嗎?這就是寶石添加的內容:笑臉:
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/394834.html
標籤:红宝石轨道
