导轨回调没有得到执行
有关我的生活中,我试图找出为什么我的回调没有得到执行有时
(你没有听错的有时作为大部分时间它的工作原理开箱即用)
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)
我只有父/ 2模型之间的亲子关系。
All I have is parent/child relations between 2 models
在创建子记录所有我做的 after_create
的回调是更新(积聚在父字段的所有子量,以避免在运行时重查询)金额字段在父表/模型记录
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
父模型(付款
)
儿童模式(销售交易
)
付款
的has_many SalesTransactions
为创建时,销售交易,我上面说更新(增量为precise)的金额
父记录(派息记录),以避免在运行时重查询,现场
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.
所以赔付量字段
只不过是所有量总和的 sales_transactions
中的支出
so Payout amount field
is nothing but summation of all amount of the sales_transactions
of that payouts
这不如说是payout.amount会(执行回调后)
it as good as saying as payout.amount would be(after callback is executed)
payout.amount == payout.sales_transactions.pluck('量')。总之
和什么我想实现使用回调
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
在这里最有意思的是,有时
时SalesTransaction是创建
的回调只是不叫我不要见支出记录的更新值
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
-
没有验证既不SalesTransaction和支出表(我已经检查了这1000次)
Payout.validators
=> []
There is not Validation neither on SalesTransaction and Payout table ( I have check this 1000 times)
Payout.validators
=> []
SalesTransaction.validators
=> []
目前,我还没有确定没有质量asssignment问题 attr_accessible
或 attr_protected
(我选中此作为同时,也为表示,工作的大部分时间,这不会一直与大众分配警告的情况下)
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)
SalesTransaction记录得到创造的一切只有支出记录没有得到更新时间(有时
)
SalesTransaction record is getting created all the time only the payouts record is not getting update(sometime
)
我已删除大部分不必要的(在这里)协会从 sales_transactions
和付款
为code简洁
I have removed most of the unwanted(over here) associations from sales_transactions
and payouts
for code brevity
没有心不是像任何事情accepts_nested_attributes_for
在任一车型
No there isnt any thing like accepts_nested_attributes_for
on either of the models
没有动态验证连接,额外的,额外的
No dynamic validations attached ,extra, extra
最后在这里,怎么我试图创建 SalesTransaction
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(选项)
尝试,而不是after_create after_save的。
Try after_save instead of after_create.