這個before_save-callback
有什麼問題?before_save中的mark_for_destruction
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 =>真正的」?看到這裏: http://weblogs.manas.com.ar/spalladino/2010/03/15/deleting-children-with-accepts_nested_attributes_for-in-rails/
也許有一些問題,如果line_item.quantity.to_f <= 0'?例如,你確定它並不總是假的嗎? – WarHog
不,我檢查'puts line_item.marked_for_destruction?'在那之後。它給出了預期的結果。但是當他們中的一個被標記時,他們都沒有得救... – NovapaX
你有沒有找到答案呢?我注意到,標記兒童物品的銷燬將導致不保存標記的bot和下一個項目(IE:-1,1,2 - 只會節省2) – bmihelac