2017-05-06 63 views
0

我正在編寫一個活動記錄事務的測試。 引發錯誤的方法如下所示;SystemStackError:堆棧級別太深 - 數據庫事務

after_save :process_transaction_fees 

def process_transaction_fees 
    if self.status == "SUCCESS" 
     transaction_fees = 0.0299 #improve after success 
     merchant = Business.find_by_id(self.business_id) 

     transaction_charges = (self.amount * transaction_fees).to_f 

     balance = merchant.balance 
     balance_before = merchant.balance 
     balance_after = (balance_before.to_f - transaction_charges.to_f) 

     merchant_deposit = BusinessTransaction.new(business_id: self.business_id ,amount: transaction_charges, balance_before: balance_before, balance_after: balance_after, status: "SUCCESS") 
     merchant.balance = (balance.to_f - transaction_charges.to_f) 

     ActiveRecord::Base.transaction do 
     merchant.save! 
     merchant_deposit.save! 
     end 
    end 
    end 

不過,我不斷收到錯誤SystemStackError: stack level too deep就行merchant.save!

這可能是什麼原因造成的?

回答

1

由於您使用的是.save!,因此它會根據after_savecallback遞歸拋出該錯誤。 解決這個問題的兩種方法已被解釋,您可以參考here

相關問題