rails callbacks not getting executed

半城伤御伤魂 提交于 2019-12-06 04:13:25

问题


For my life I trying to find out why are my callbacks are not getting executed sometimes(you heard it right sometimes as most of time it works out of the box)

All I have is parent/child relations between 2 models

upon creation of child record all I'm doing in after_create callback is update(accumulate all child amount in parent field to avoid heavy query at run time) the amount field in parent table/ model record

Parent model(Payout)

Child model is (Sales Transaction)

Payout has_many SalesTransactions as said above upon creation of sales transaction I'm updating(incrementing to be precise) the amount field of parent record (payout record) so as to avoid heavy query at run time.

so Payout amount field is nothing but summation of all amount of the sales_transactions of that payouts

it as good as saying as payout.amount would be(after callback is executed)

payout.amount == payout.sales_transactions.pluck('amount').sum

and that what I trying to achieve using callbacks

class SalesTransaction < ActiveRecord::Base
   belongs_to :payout
   after_create :update_payout_for_sale

   def update_payout_for_sale
    sales_amount = payout.amount || 0
    sales_amount =  sales_amount + amount.to_f
    ## Also make note of the minus from original amount i.e refund and custom_deduction_amount
    payout.update_attributes(:amount => sales_amount)
  end  

end

class Payout < ActiveRecord::Base
  has_many :sales_transactions
  has_one :referrer
  after_save :update_referrer_earning

  def update_referrer_earning
    referrer.update_attributes(:amount  => (amount*10)/100.to_d)) rescue nil
  end
end

The interesting part over here is that sometime when SalesTransaction is created the callback is just not called as I dont see the update value of the in payouts record

I'm trying to avoid the callback for now but for the sake knowing why the callback is not getting executed has led me to ask this question

NOTE

  1. There is not Validation neither on SalesTransaction and Payout table ( I have check this 1000 times) Payout.validators => []

    SalesTransaction.validators => []

  2. There is no mass asssignment issue as I havent define attr_accessible or attr_protected (I Check this as well and also as said it work most time which wouldn't have been the case with mass-assignment warning)

  3. SalesTransaction record is getting created all the time only the payouts record is not getting update(sometime)

  4. I have removed most of the unwanted(over here) associations from sales_transactions and payouts for code brevity

  5. No there isnt any thing like accepts_nested_attributes_for on either of the models

  6. No dynamic validations attached ,extra, extra

Lastly Here how I'm trying to create the SalesTransaction

  options =  {"performer_id"=>177, "customer_id"=>35526, "sale_type"=>"sale", "show_id"=>502, "performer_percentage"=>BigDecimal.new("40.0"), "show_duration"=>4104, "gross_credits"=>3754, "gross_sales"=>BigDecimal.new("375.4"), "amount"=>BigDecimal.new("150.16"), "affiliate_id"=>nil, "affiliate_earning"=>BigDecimal.new("0.0"), "total_profit"=>BigDecimal.new("225.24"), "payout_period_id"=>89,"payout_id"=>4156, "stream_connection_id"=>540572, "history_id"=>44575, "credits"=>{:when_show_started=>350, :purchased_during_show=>{:free=>[], :paid=>[]}, :total_consumed=>{:free=>350, :paid=>3754}}, "sliding_scale_recalculations_done"=>false, "paid_minutes"=>62.57}

SalesTransaction.create(options)


回答1:


I wanted to leave a comment in response to Viren. Try SalesTransaction.create! (with the exclamation mark). Do you then see the mass-assignment error consistently?




回答2:


Try after_save instead of after_create.



来源:https://stackoverflow.com/questions/17955052/rails-callbacks-not-getting-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!