【问题标题】:Stack Level Too Deep, Maybe recursive but not sure堆栈级别太深,可能递归但不确定
【发布时间】:2014-02-04 19:50:05
【问题描述】:

我收到Stack Level too deep 错误,我认为这与以下代码有关,但我不知道如何修复它:

   after_save :update_milestone

   def update_milestone
      if order % 50 == 0
         self.update_attributes(is_milestone: true)
      else
         self.update_attributes(is_milestone: false)
      end
   end

任何想法

【问题讨论】:

  • 而不是使用带有两个更新的 if 语句......为什么不在更新中使用条件的值?像这样:self.update_attributes(is_milestone: order % 50 == 0)

标签: ruby-on-rails model callback stack-level


【解决方案1】:

您正在调用 update_attributes 来进行验证。然后这会触发导致错误的回调 after_save。

你想要:

self.update_column(:is_milestone, value)

这不会触发验证。

【讨论】:

  • after_save -> update_milestone -> self.update_attributes -> after_save -> update_milestone -> self.update_attributes -> ...
  • 感谢您的建议!
猜你喜欢
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-04
  • 2013-10-05
  • 2011-11-17
  • 2012-07-24
  • 2016-08-02
相关资源
最近更新 更多