Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 为什么这是一个无止境的循环?活动记录 类帐户:更新 def给予用户信用 信用=User.current\u User.credit+3.8 User.current_User.update_属性(:credit,credit) 结束 结束_Ruby_Activerecord_Model - Fatal编程技术网

Ruby 为什么这是一个无止境的循环?活动记录 类帐户:更新 def给予用户信用 信用=User.current\u User.credit+3.8 User.current_User.update_属性(:credit,credit) 结束 结束

Ruby 为什么这是一个无止境的循环?活动记录 类帐户:更新 def给予用户信用 信用=User.current\u User.credit+3.8 User.current_User.update_属性(:credit,credit) 结束 结束,ruby,activerecord,model,Ruby,Activerecord,Model,当我使用它时,服务器会挂起,当我在完全重新启动后返回应用程序时,我的信用额度为1000英镑 这是怎么回事 谢谢:D在我看来,您正在设置:每次更新记录时都要运行“用户信用回调” 但是由于回调会更新记录,因此它会再次触发回调,回调将继续进行…您也可以使用此私有方法: class Account < ActiveRecord::Base after_update :give_user_credit, :on => :update def give_user_credit

当我使用它时,服务器会挂起,当我在完全重新启动后返回应用程序时,我的信用额度为1000英镑

这是怎么回事


谢谢:D

在我看来,您正在设置
:每次更新记录时都要运行“用户信用回调”


但是由于回调会更新记录,因此它会再次触发回调,回调将继续进行…

您也可以使用此私有方法:

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

感谢我将逻辑移到了控制器中,现在它工作正常了,可惜3112.20英镑是假的:P@Karl在_save:on=>:update,然后不调用update_属性…,怎么样。。。?
model.credit = 10
model.send(:update_without_callbacks)