开发者

Why is this an endless loop? ActiveRecord

class Account < ActiveRecord::Base
  after_update :give_user_credit, :on => :update 

  def give_user_credit
    credit = User.current_user.credit + 3.8
    User.current_user.update_attribute(:credit, credit)
  end
end

When I use this the server hangs and when I come back to the application after a full reboot my credit is in t开发者_如何学Che £1000's.

Whats going on here..

Thanks :D


Looks to me like you are setting the :give_user_credit callback to run every time that the record is updated.

But since the callback updates the record, it then triggers the callback again, which will continue on and on...


You could also use this private method:

model.credit = 10
model.send(:update_without_callbacks)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜