简体   繁体   English

Ruby on Rails 中的堆栈级别太深错误

[英]Stack level too deep error in Ruby on Rails

I'm having a stack level too deep error using Ruby 1.8.7 with Rails 3.0.4 and with the rails console I performed the following commands.我在使用带有 Rails 3.0.4 的 Ruby 1.8.7 和使用 Rails 控制台时遇到堆栈级别太深错误,我执行了以下命令。

leo%>rails console
Loading development environment (Rails 3.0.4)
ruby-1.8.7-head > leo = Organization.find(1)

SystemStackError: stack level too deep
from /app/models/organization.rb:105:in `parents'

Here is the object that is having issues..这是有问题的对象..

class Organization < ActiveRecord::Base

  has_many                  :group_organizations, :dependent =>
:delete_all
  has_many                  :groups, :through => :group_organizations

  has_many                  :orders
  has_many                  :product_contracts

  has_many                  :people
  accepts_nested_attributes_for :people

  has_many                  :addresses
  accepts_nested_attributes_for :addresses

  has_many                  :organizations
  has_many                  :departments
  has_many                  :organization_credits

  has_many                  :documents

  validates_presence_of     :name



    def self.parents
      @organizations = Organization.where("is_company = ?",true)
      #@organization_parents = []
      select_choice = I18n.t("select") + " "+ I18n.t("segments.description")
      @organization_parents = [select_choice]
      for organization in @organizations
        @organization_parents << [organization.name, organization.id]
      end
      return @organization_parents
    end

This error generally happens when you accidentally recursively changing an attribute.当您不小心递归更改属性时,通常会发生此错误。 If you have a username attribute in User model, and a virtual attribute named username, that is directly changing the username, you end up calling the virtual, the virtual calls the virtual again and so on.. Therefore, take a look on whether something like that happens somewhere in your code.如果你在 User 模型中有一个 username 属性,还有一个名为 username 的虚拟属性,就是直接改变用户名,你最终调用了虚拟,虚拟又调用了虚拟等等..所以,看看是否有什么就像在您的代码中的某个地方发生的那样。

The stack level too deep error occurs also, if you want to destroy a record and you have an association with :dependent => :destroy to another model.如果您想销毁记录并且您与:dependent => :destroy关联到另一个模型,也会发生堆栈级别太深错误。 If the other model has a association with :dependent => :destroy back to this model, the stack level is too deep , too.如果另一个模型与:dependent => :destroy关联到这个模型,那么堆栈级别也太深了。

I had a "stack-level too deep" issue too.我也有一个"stack-level too deep"问题。 it was due to recursiveness in one of my functions and had been caused by a typo as you can see from below:这是由于我的一个函数中的递归性造成的,并且是由打字错误引起的,如下所示:

def has_password?(submitted_password)
  encrypt_password == encrypt(submitted_password)
end

private

def encrypt_password
  self.salt = make_salt unless has_password?(password)
  self.encrypted_password = encrypt(password)
end

I realised I had to change the second line to encrypted and it worked.我意识到我必须将第二行更改为加密并且它起作用了。 Just checkout for recursion in your code it must be happening somewhere.只需在您的代码中签出递归,它一定发生在某处。 Unfortunately I can't be of better use since I can't look at all your code files.不幸的是,我不能更好地使用,因为我无法查看您的所有代码文件。

I was getting same stack level too deep error & it turns out that the issue was of recurring rendering of a partial.我得到了相同的堆栈级别太深的错误,结果证明问题是部分的重复渲染。

I happened to call render a_partial in main view and then in the partial, I accidentally called the same partial again.我碰巧在主视图中调用了 render a_partial,然后在局部视图中,我不小心再次调用了相同的局部视图。

HTH HTH

As you are not showing all the code, I can only speculate that you have defined inspect or to_s to build a string containing, among other things the parents.由于您没有显示所有代码,我只能推测您已经定义了inspectto_s来构建一个包含父级等内容的字符串。

Your current parents method doesn't seem to be doing anything reasonable, as it returns all organisations that are companies, no matter which association you start from.您当前的parents方法似乎没有做任何合理的事情,因为它返回所有公司组织,无论您从哪个协会开始。 Thus, any company has itself as parent.因此,任何公司都以自己为母公司。 Attempting to convert it to string will induce an infinite loop to try to show the parents' of the parents' of ...尝试将其转换为字符串将导致无限循环,以尝试显示 ...

In any case, the bulk of your parents method should be in a helper, called something like options_for_parents_select , because that's what it seems to be doing?在任何情况下,你的parents方法的大部分应该在一个帮助器中,称为options_for_parents_select ,因为它似乎在做什么? Even then, the first empty choice should be passed as allow_null to select.即便如此,第一个空选项也应该作为allow_null传递给选择。

The fact that it sets instance variables is a code smell.它设置实例变量的事实是一种代码味道。

Good luck祝你好运

If you are getting this error it means rails version that you are using in your application is not compatible with Ruby Version.如果您收到此错误,则表示您在应用程序中使用的 rails 版本与 Ruby 版本不兼容。

Solutions you can use to solve this issue.可用于解决此问题的解决方案。

1) You need to downgrade the ruby version to older version. 1) 您需要将 ruby​​ 版本降级到旧版本。

2) or you need to upgrade Rails to latest version. 2) 或者您需要将 Rails 升级到最新版本。

I've found the solution to this issue...我已经找到了这个问题的解决方案......

I'm using Rails 3 and my class looks like this (and the problematic methods was this too)我正在使用 Rails 3,我的班级看起来像这样(有问题的方法也是这样)

class Organization < ActiveRecord::Base
  def self.parents
    @organizations = self.find :all, :conditions => ['is_company = ? ',true]
    select_choice = I18n.t("select") + " "+ I18n.t("segments.description")
    @organization_parents = [select_choice]
    for organization in @organizations
      @organization_parents << [organization.name, organization.id]
    end
    return @organization_parents
  end 
  #...
end

I did have to hack a lot in the code to find out something was wrong with the named_scope on the line我确实必须在代码中进行大量修改,以找出该行上的 named_scope 有问题

@organizations = self.find :all, :conditions => ['is_company = ? ',true]

So I had to change it to something like this所以我不得不把它改成这样

@organizations = Organization.where("is_company = ?",true)

But it was wrong too.. So I decided to add an scope for this below the class name so the final code looks like this:但它也错了..所以我决定在类名下面添加一个范围,这样最终的代码看起来像这样:

class Organization < ActiveRecord::Base
  scope :company, where("is_company = ?",true)

  def self.parents
    @organizations = self.company
    select_choice = I18n.t("select") + " "+ I18n.t("segments.description")
    @organization_parents = [select_choice]
    for organization in @organizations
      @organization_parents << [organization.name, organization.id]
    end
    return @organization_parents
  end 
  #...
end

So using this line with the scope所以使用这一行与范围

@organizations = self.company

it worked flawlessly in every part of the code.它在代码的每个部分都完美无缺。

I was wondering if the named_scope is deprecated when using class methods or they are not supported from now and throws an error and not a warning before我想知道在使用类方法时是否不推荐使用 named_scope 或者从现在开始不支持它们并抛出错误而不是之前的警告

Thanks for your help Leo谢谢你的帮助狮子座

I got this error when incorrectly creating a has_many relationship like this:我在错误地创建这样的 has_many 关系时收到此错误:

has_many :foos, through: foo

So don't put the same model as 'through' or it will loop endlessly.所以不要把相同的模型作为“通过”,否则它会无休止地循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM