2016-04-23 13 views
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 

p1 = Part.createを作成すると、part2オブジェクトも作成されます。しかし、それはpart2サブジョブの2倍の数を更新します。私はpart2オブジェクトの履歴トラッカーをチェックしました。 2つの履歴トラッカーが表示されますが、dbには1つのpart2オブジェクトのみが表示されます。どんな助けも素晴らしいだろう。保存後コールバックを2回実行した後

答えて

0

はまず、私はあなたのエラー/問題が何であるかではない百パーセントだけど、私はあなたのコードで何が起こっているのかを走ることに役立ちます期待しています:

を私がtrueにready_for_invoiceデフォルトを想定しています。

は、したがって、あなたがコード化されたことは言っている:

は、新しい部品を作成し、

(それがないもの)ready_for_invoice = trueの場合、保存した後、真

p1 = Part.create 

にready_for_invoiceを設定

# run update_inventory first 

新しい部分を作成し、ready_を設定します。

# run update_total_qty (doing whatever that does) 
P1保存した後、P2

# run update_total_qty (doing whatever that does) 

を保存した後、偽

p2 = Part.create(ready_for_invoice: false) 

にfor_invoice

関連する問題