Order has_many RentalItems
Rails - 未更改的屬性已註冊爲已更改?
所以我對Order
此回調,其中一點是,xyz_method
運行,只要任何孩子RentalItem
有size
或specification
改變,或一個新的子RentalItem
已被添加。
before_save do
if rental_items_attributes_modified?
xyz_method
end
end
def rental_items_attributes_modified?
self.rental_items.each do |ri|
# as long as ONE item had ONE thing changed, we return true or it's a new record
puts "in RI modified?"
puts "#{ri.new_record?} with ID #{ri.id}"
puts "#{ri.specification_changed?} from #{ri.specification_was} to #{ri.specification}"
puts "#{ri.size_changed?} from #{ri.size_was} to #{ri.size}"
if ri.specification_changed? || ri.size_changed? || ri.new_record?
return true
end
end
return false
end
所有puts
都幫我調試...我不知道爲什麼,但rental_items_attributes_modified?
保持返回true,因爲對於RentalItems
之一,specification_changed?
回報true
即使它不是...這是有關日誌輸出:
# testing the first child RI
in RI modified?
false with ID 1
false from blue to blue
false from Regular to Regular
# testing the second child RI
in RI modified?
false with ID 2
true from to # <<< why is this happening???
false from Regular to Regular
對於怪異的第二個孩子RI
問題,specification = ""
最初,什麼被傳遞是PARAMS這樣的:
"rental_items_attributes"=>[{"id"=>"2", "specification"=>"", "size"=>"Regular"}]
我已經試過這個獨立的控制檯,它觸發正確的行爲...
# for an order whose child RI initially had a blank specification & size
o.update_attributes({"rental_items_attributes" => [{"id" => 79, "specification" => ""}]})
=>
in RI modified?
false with ID 79
false from to
false from to
「規格」是否有默認值?這可能是零與「」的問題。 –
我看着它,有同樣的懷疑,這不是問題;當孩子第一次創建時''''規格'開頭爲'''' – james