0
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.

Siva Gollapalli
  • 626
  • 6
  • 20

1 Answers1

0

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

p1 = Part.create

after saving, if ready_for_invoice = true (which it does)

# run update_inventory first

create a new part, set ready_for_invoice to false

p2 = Part.create(ready_for_invoice: false)

after saving p2

# run update_total_qty (doing whatever that does)

after saving p1

# run update_total_qty (doing whatever that does)
ABrowne
  • 1,574
  • 1
  • 11
  • 21