[英]Stack level too deep error in Ruby on Rails
我在使用带有 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'
这是有问题的对象..
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
当您不小心递归更改属性时,通常会发生此错误。 如果你在 User 模型中有一个 username 属性,还有一个名为 username 的虚拟属性,就是直接改变用户名,你最终调用了虚拟,虚拟又调用了虚拟等等..所以,看看是否有什么就像在您的代码中的某个地方发生的那样。
如果您想销毁记录并且您与:dependent => :destroy
关联到另一个模型,也会发生堆栈级别太深错误。 如果另一个模型与:dependent => :destroy
关联到这个模型,那么堆栈级别也太深了。
我也有一个"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
我意识到我必须将第二行更改为加密并且它起作用了。 只需在您的代码中签出递归,它一定发生在某处。 不幸的是,我不能更好地使用,因为我无法查看您的所有代码文件。
我得到了相同的堆栈级别太深的错误,结果证明问题是部分的重复渲染。
我碰巧在主视图中调用了 render a_partial,然后在局部视图中,我不小心再次调用了相同的局部视图。
HTH
由于您没有显示所有代码,我只能推测您已经定义了inspect
或to_s
来构建一个包含父级等内容的字符串。
您当前的parents
方法似乎没有做任何合理的事情,因为它返回所有公司组织,无论您从哪个协会开始。 因此,任何公司都以自己为母公司。 尝试将其转换为字符串将导致无限循环,以尝试显示 ...
在任何情况下,你的parents
方法的大部分应该在一个帮助器中,称为options_for_parents_select
,因为它似乎在做什么? 即便如此,第一个空选项也应该作为allow_null
传递给选择。
它设置实例变量的事实是一种代码味道。
祝你好运
如果您收到此错误,则表示您在应用程序中使用的 rails 版本与 Ruby 版本不兼容。
可用于解决此问题的解决方案。
1) 您需要将 ruby 版本降级到旧版本。
2) 或者您需要将 Rails 升级到最新版本。
我已经找到了这个问题的解决方案......
我正在使用 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
我确实必须在代码中进行大量修改,以找出该行上的 named_scope 有问题
@organizations = self.find :all, :conditions => ['is_company = ? ',true]
所以我不得不把它改成这样
@organizations = Organization.where("is_company = ?",true)
但它也错了..所以我决定在类名下面添加一个范围,这样最终的代码看起来像这样:
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
所以使用这一行与范围
@organizations = self.company
它在代码的每个部分都完美无缺。
我想知道在使用类方法时是否不推荐使用 named_scope 或者从现在开始不支持它们并抛出错误而不是之前的警告
谢谢你的帮助狮子座
我在错误地创建这样的 has_many 关系时收到此错误:
has_many :foos, through: foo
所以不要把相同的模型作为“通过”,否则它会无休止地循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.