Order has_many RentalItems
Rails - 変更されていない属性は変更されたとして登録されていますか?
子RentalItem
はそのsize
またはspecification
変更を持っている、または新しい子RentalItem
が追加されました。いつでもポイントがxyz_method
実行されることですだから私はOrder
にこのコールバックを持っています
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?
はRentalItems
のONE、ことはありませんにもかかわらずspecification_changed?
戻っtrue
が...これが関連しているので、trueを返し続けます当初specification = ""
、質問に奇妙な二子RI
について
# 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
、そして何が渡されると、このようなの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
'specification'のデフォルト値はありますか?これはnil対 ""の問題かもしれません。 –
私はそれを見て、同じ疑惑を持っていた、それは問題ではない。子どもが最初に作成されたときに '' specification 'は '' 'として始まります – james