- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
这个 before_save-callback
有什么问题?
class Order < ActiveRecord::Base
has_many :line_items, :dependent => :destroy, :inverse_of => :order
accepts_nested_attributes_for :line_items
attr_accessible :line_items_attributes
before_save :mark_line_items_for_removal
def mark_line_items_for_removal
line_items.each do |line_item|
line_item.mark_for_destruction if line_item.quantity.to_f <= 0
end
end
end
当其中一个 line_items
被标记为销毁时,不会保存任何 line_item
。但是,父 Order 对象确实得到了保存。返回 true 没有什么区别...
关于 mark_for_destruction:http://apidock.com/rails/v3.1.0/ActiveRecord/AutosaveAssociation/mark_for_destruction为什么用它代替“:allow_destroy => true”?看这里: http://weblogs.manas.com.ar/spalladino/2010/03/15/deleting-children-with-accepts_nested_attributes_for-in-rails/
最佳答案
我相信您需要为 has_many 定义设置 :autosave => true 选项。
如前所述,这里:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many
“如果为 true,则在保存父对象时始终保存关联对象,如果标记为销毁则销毁它们。如果为 false,则永远不会保存或销毁关联对象。默认情况下,仅保存作为新记录的关联对象。”
关于ruby-on-rails - mark_for_destruction 在 before_save,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8100763/
这个 before_save-callback 有什么问题? class Order :destroy, :inverse_of => :order accepts_nested_attri
在我的 rails 应用程序中,子记录获得 marked_for_destruction我想撤消该操作并取消标记它们。有办法吗? 提前致谢。 最佳答案 child.reload if child.ma
我是一名优秀的程序员,十分优秀!