为什么这是一个无限循环? 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 

当我使用这个服务器挂起,当我完全重新启动后回到应用程序时,我的功劳在1000英镑。为什么这是一个无限循环? ActiveRecord

怎么回事这里..

感谢:d

看起来像你对我的设置:give_user_credit callback运行每次记录更新时间。

但由于回调更新记录,它再次触发回调,这将继续和...

+0

感谢您的IVE把逻辑转移到了控制器中,现在它的工作正常,耻辱£3112.20是假的:P – 2010-06-10 21:47:03

+0

@Karl如何before_save:on =>:update然后就是不要调用update_attribute ...? – hurikhan77 2010-06-10 22:03:18

你也可以使用这个私有方法:

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