Rails callbacks fail

In my life I’m trying to find out why my callbacks aren’t sometimes executed (you heard that this is sometimes correct, since most of the time it works out of the box)

All I have is the relationship between parents and children between two models

when creating a child record, all I do in the after_create callback is the update (accumulates the entire child amount in the parent field to avoid heavy query at runtime) the sum field in the parent table / model record

Parent Model ( Payout )

Child Model ( Sales Transaction )

Payout has_many SalesTransactions , as described above, when creating a sales transaction. I am updating (increasing, more precisely) the amount field of the parent record (payout record) to avoid heavy requests at runtime.

therefore, the amount field payments are nothing more than the summation of all the sales_transactions these payments

it's as good as saying payout.amth will be (after the callback is done)

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

and what I'm trying to achieve with 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 ie 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 here is that sometime , when SalesTransaction is created , the callback is simply not called, since I do not see the update value in the payout record

I am trying to avoid the callback at the moment, but knowing why callback is not getting executed, led me asks this question

Note

  • There is no validation in the SalesTransaction and Payout table (I check this 1000 times) Payout.validators => []

    SalesTransaction.validators => []

  • There is no question of a mass dispute, since I cannot determine attr_accessible or attr_protected (I will also check this, and also said that it works most of the time, which would not be the case with a warning of mass assignment)

  • SalesTransaction records are created all the time, only the payout record does not receive an update ( sometime )

  • I removed most of the unwanted (here) associations from sales_transactions and payouts for code brevity

  • There is no such thing as accepts_nested_attributes_for on any of the models

  • No dynamic verification add-ons, extra, extra

Finally, this is how I try to create a 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)

+4
source share
2 answers

I wanted to leave a comment in response to Viren. Try SalesTransaction.create! (with an exclamation mark). Then you sequentially see a mass assignment error?

0
source

Try after_save instead of after_create.

0
source

Source: https://habr.com/ru/post/1494357/


All Articles