Note that there are some explanatory texts on larger screens.

plurals
  1. POrails callbacks not getting executed
    text
    copied!<p>For my life I trying to find out why are my callbacks are not getting executed <code>sometimes</code>(you heard it right <strong>sometimes</strong> as most of time it works out of the box)</p> <p>All I have is parent/child relations between 2 models </p> <p>upon creation of child record all I'm doing in <code>after_create</code> 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</p> <p>Parent model(<code>Payout</code>) </p> <p>Child model is (<code>Sales Transaction</code>) </p> <p><code>Payout</code> <strong>has_many</strong> <code>SalesTransactions</code> as said above upon creation of sales transaction I'm updating(incrementing to be precise) the <code>amount</code> field of parent record (payout record) so as to avoid heavy query at run time.</p> <p>so Payout <code>amount field</code> is nothing but summation of all amount of the <code>sales_transactions</code> of that payouts</p> <p>it as good as saying as payout.amount would be(after callback is executed)</p> <p><code>payout.amount == payout.sales_transactions.pluck('amount').sum</code> </p> <p>and that what I trying to achieve using callbacks</p> <pre><code>class SalesTransaction &lt; 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 =&gt; sales_amount) end end class Payout &lt; ActiveRecord::Base has_many :sales_transactions has_one :referrer after_save :update_referrer_earning def update_referrer_earning referrer.update_attributes(:amount =&gt; (amount*10)/100.to_d)) rescue nil end end </code></pre> <p>The interesting part over here is that <code>sometime</code> when SalesTransaction is <code>created</code> the callback is just not called as I dont see the update value of the in payouts record </p> <p>I'm trying to avoid the callback for now but for the sake knowing <code>why</code> the callback is not getting executed has <code>led me</code> to ask this question</p> <h2>NOTE</h2> <ol> <li><p>There is not Validation neither on SalesTransaction and Payout table ( I have check this 1000 times) <code>Payout.validators</code> => []</p> <p><code>SalesTransaction.validators</code> => []</p></li> <li><p>There is no mass asssignment issue as I havent define <code>attr_accessible</code> or <code>attr_protected</code> (I Check this as well and also as said it work most time which wouldn't have been the case with mass-assignment warning)</p></li> <li><p>SalesTransaction record is getting created all the time only the payouts record is not getting update(<code>sometime</code>)</p></li> <li><p>I have removed most of the unwanted(over here) associations from <code>sales_transactions</code> and <code>payouts</code> for code brevity </p></li> <li><p>No there isnt any thing like <code>accepts_nested_attributes_for</code> on either of the models </p></li> <li><p>No dynamic validations attached ,extra, extra </p></li> </ol> <p>Lastly Here how I'm trying to create the <code>SalesTransaction</code></p> <pre><code> options = {"performer_id"=&gt;177, "customer_id"=&gt;35526, "sale_type"=&gt;"sale", "show_id"=&gt;502, "performer_percentage"=&gt;BigDecimal.new("40.0"), "show_duration"=&gt;4104, "gross_credits"=&gt;3754, "gross_sales"=&gt;BigDecimal.new("375.4"), "amount"=&gt;BigDecimal.new("150.16"), "affiliate_id"=&gt;nil, "affiliate_earning"=&gt;BigDecimal.new("0.0"), "total_profit"=&gt;BigDecimal.new("225.24"), "payout_period_id"=&gt;89,"payout_id"=&gt;4156, "stream_connection_id"=&gt;540572, "history_id"=&gt;44575, "credits"=&gt;{:when_show_started=&gt;350, :purchased_during_show=&gt;{:free=&gt;[], :paid=&gt;[]}, :total_consumed=&gt;{:free=&gt;350, :paid=&gt;3754}}, "sliding_scale_recalculations_done"=&gt;false, "paid_minutes"=&gt;62.57} </code></pre> <p><code>SalesTransaction.create(options)</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload