Ruby-on-rails 为什么我的 rails3 beta4 模型出现“SystemStackError: stack level too deep”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3270488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:55:04  来源:igfitidea点击:

Why am I getting a "SystemStackError: stack level too deep" in my rails3 beta4 model

ruby-on-railsrubyactiverecordruby-on-rails-3

提问by Ekampp

I'm getting the following error: SystemStackError: stack level too deepwhen executing the following code in rails3 beta4under ruby 1.9.2-rc1:

我收到以下错误:SystemStackError: stack level too deep在执行以下代码时,rails3 beta4ruby 1.9.2-rc1

ruby-1.9.2-rc1 > f = Forum.all.first
  => #<Forum id: 1, title: "Forum 1", description: "Description 1", content: "Content 1", parent_id: nil, user_id: 1, forum_type: "forum", created_at: "2010-07-17 04:39:41", updated_at: "2010-07-17 04:39:41", icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, icon_updated_at: nil> 
ruby-1.9.2-rc1 > f.children
  => [#<Forum id: 2, title: "Thread 2", description: "Description 2", content: "Content 2", parent_id: 1, user_id: 1, forum_type: "thread", created_at: "2010-07-17 04:40:17", updated_at: "2010-07-17 04:40:17", icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, icon_updated_at: nil>] 
ruby-1.9.2-rc1 > f.forum_type = "thread"
  => "thread" 
ruby-1.9.2-rc1 > f.save
SystemStackError: stack level too deep
from /Users/emilkampp/.rvm/rubies/ruby-1.9.2-rc1/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!!
ruby-1.9.2-rc1 > 

And that is caused by the following code:

这是由以下代码引起的:

# Before and after filters
# 
before_update :update_all_nested_objects, :if => :forum_type_changed?

protected
  # Checks if the +forum_type+ has been changed
  # 
  def forum_type_changed?
    self.forum_type_changed?
  end

  # Updates all nested upjects if the +forum_type+ has been changed
  # 
  # This will trigger the +update_all_nested_objects+ method on all touched children, thus 
  # starting a chain-reaction all the way through the forum-tree.
  # 
  # NOTE: This is where the error is triggered, since this is the only non-tested looping code, and my test-
  #       cases hasn't changed since this was added.
  # 
  def update_all_nested_objects
    children.each do |child|
      child.forum_type = child_type
      child.save
    end
  end

So, what's going on. I have been checking around a little, but nobody seems to have the same problem. Either it's not the same ruby version, thus the workflow.rb file is different, or the stack level to deepis caused by something else.

发生什么了。我一直在检查,但似乎没有人有同样的问题。要么不是同一个ruby 版本,因此workflow.rb 文件不同,要么stack level to deep是其他原因造成的。

Any help will be greatly apreciated!

任何帮助将不胜感激!

Best regards

此致

// Emil

// 埃米尔

回答by Salil

You are calling same method in method itself

您在方法本身中调用相同的方法

  def forum_type_changed?
    self.forum_type_changed?  #This will never ending process hence it gives error
  end

I think you have same method nameand column namethat causing problem change your method name then

我觉得你有相同的method namecolumn name这造成的问题改变你的方法名称,然后

  def check_forum_type_changed?
    self.forum_type_changed?  #method name and column name are different
  end