class SubJob
field :total_qty
end
class Part
belongs_to :sub_job
after_save :update_inventory, if: ready_for_invoice
after_save :update_total_qty
def update_inventory
# creating one more part2
part2 = Part.create(ready_for_invoice: false)
end
def update_total_qty
# updating total qty on sub job
end
end
when I creating p1 = Part.create it creates part2 object as well. But it updates qty twice for part2 sub job. I have checked history trackers for part2 object. It show two history trackers but only one part2 object on db. Any help would be great.
Firstly, I'm not hundred percent on what your error/issue is, but I am hoping running through what is going on in your code will help:
I am assuming ready_for_invoice defaults to true.
Therefore what you have coded is saying:
create a new part, set ready_for_invoice to true
after saving, if ready_for_invoice = true (which it does)
create a new part, set ready_for_invoice to false
after saving p2
after saving p1