ruby-on-rails - 为什么我的 rails3 beta4 模型中出现 "SystemStackError: stack level too deep"

标签 ruby-on-rails ruby activerecord ruby-on-rails-3

ruby 1.9.2-rc1< 下的 rails3 beta4 中执行以下代码时,出现以下错误:SystemStackError: stack level too deep/:

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 > 

这是由以下代码引起的:

# 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

所以,这是怎么回事。我一直在检查周围,但似乎没有人遇到同样的问题。不是相同的 ruby​​ 版本,因此 workflow.rb 文件不同,或者 stack level to deep 是由其他原因引起的。

我们将不胜感激任何帮助!

最好的问候

//埃米尔

最佳答案

你在方法本身调用相同的方法

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

我认为你有相同的 method namecolumn name 导致问题然后更改你的方法名称

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

关于ruby-on-rails - 为什么我的 rails3 beta4 模型中出现 "SystemStackError: stack level too deep",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3270488/

相关文章:

html - 如何使用 Rails Nokogiri gem 提取 HTML 中的数据属性?

mysql - 在 Heroku CI 上使用 MySQL

ruby - 在UDPSocket#bind中救援ERRNO::EADDRINUSE

ruby-on-rails - 从我的 Controller 更新数据库字段时出现问题

ruby-on-rails - 自定义操作未显示在 Rails_admin 仪表板上

ruby-on-rails - 如何在 Arel 和 Rails 中执行 LIKE 查询?

ruby-on-rails-3 - 神秘的 SELECT COUNT(*) 出现在我的 Controller 中,用于 has_many 关系

sql - 加入多个表/约束或 has_one_through 与次要约束

ruby-on-rails - Rails 模型创建和新建在测试环境中返回 nil

ruby - 运行时间 Array#unshift 与 Array#shift