本文介绍了Rails Stack Level Too Deep with after_update 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做到这样,如果用户更改了两个布尔变量(buyerseller)中的任何一个,它们可以在任一方向更改以触发action) 它将重置第三个变量 status_confirmed 的状态为 false.

I'm trying to make it so that if a user changes either of two boolean variables (buyer and seller, they can be changed in either direction to trigger the action) it will reset the status of a third variable status_confirmed to false.

我的 user 模型中有以下内容:

I have the following in my user model:

after_update :reset_confirmed

  def reset_confirmed
    if self.buyer_changed? || self.seller_changed?
      self.update_attributes(status_confirmed: false)
    end
  end

据我所知,stack level too deep 错误是由于无限循环或递归引起的,我找不到.谁能看出我哪里出错了?

From what I can understand stack level too deep errors are due to infinite loops or recursions, which I can't find. Can anyone see where I'm going wrong?

推荐答案

2 种处理方法:

after_update :reset_confirmed
def reset_confirmed
  self.update_column(:status_confirmed, false) if self.buyer_changed? || self.seller_changed?
end

update_attributeupdate_column 之间的区别对您有帮助,因为后者跳过回调(如果有).

The difference between update_attribute and update_column is what will help you, as the latter skips callbacks if any.

before_save  :reset_confirmed
def reset_confirmed
  self.status_confirmed = false if self.buyer_changed? || self.seller_changed?
end

在这里,您只是在将其保存到数据库之前分配一个值,所以...

Here, you are just assigning a value before saving it to db, so...

希望有帮助..

这篇关于Rails Stack Level Too Deep with after_update 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:19