Ruby on Rails 中的堆栈级别太深错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5446055/
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
Stack level too deep error in Ruby on Rails
提问by Leo
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
采纳答案by Leo
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
谢谢你的帮助狮子座
回答by Spyros
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 的虚拟属性,就是直接改变用户名,你最终调用了虚拟,虚拟又调用了虚拟等等..所以,看看是否有什么就像在您的代码中的某个地方发生的那样。
回答by Christian
The stack level too deeperror occurs also, if you want to destroy a record and you have an association with :dependent => :destroyto another model. If the other model has a association with :dependent => :destroyback to this model, the stack level is too deep, too.
该堆栈层次过深,如果你想摧毁一个记录和你有关联的错误也会发生,:dependent => :destroy到另一种模式。如果其他模型与:dependent => :destroy返回此模型有关联,则堆栈级别也太深。
回答by Lym
I had a "stack-level too deep"issue too. it was due to recursiveness in one of my functions and had been caused by a typo as you can see from below:
我也有"stack-level too deep"问题。这是由于我的一个函数中的递归性造成的,并且是由打字错误引起的,如下所示:
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.
我意识到我必须将第二行更改为加密并且它起作用了。只需在您的代码中签出递归,它一定发生在某处。不幸的是,我不能更好地使用,因为我无法查看您的所有代码文件。
回答by Pradeep S
I was getting same stack level too deeperror & 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
回答by Marc-André Lafortune
As you are not showing all the code, I can only speculate that you have defined inspector to_sto build a string containing, among other things the parents.
由于您没有显示所有代码,我只能推测您已经定义inspect或to_s构建了一个字符串,其中包含父项。
Your current parentsmethod doesn't seem to be doing anything reasonable, as it returns all organisations that are companies, no matter which association you start from. 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 ...
您当前的parents方法似乎没有做任何合理的事情,因为它返回所有作为公司的组织,无论您从哪个协会开始。因此,任何公司都以自己为母公司。尝试将其转换为字符串将导致无限循环,以尝试显示 ...
In any case, the bulk of your parentsmethod should be in a helper, called something like options_for_parents_select, because that's what it seems to be doing? Even then, the first empty choice should be passed as allow_nullto select.
在任何情况下,您的大部分parents方法都应该在一个助手中,称为类似的东西options_for_parents_select,因为这似乎是在做什么?即便如此,第一个空的选择应该作为allow_null选择传递。
The fact that it sets instance variables is a code smell.
它设置实例变量的事实是一种代码味道。
Good luck
祝你好运
回答by Hitesh Ranaut
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 升级到最新版本。

