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  :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的虚拟属性,那就是直接更改用户名,你最终调用虚拟,虚拟再次调用虚拟等等。所以,看看是否有什么东西就像在你的代码中某处发生的那样。

如果你想销毁一条记录并且你有一个关联: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 

我意识到我必须将第二行改为加密而且它有效。 只需在代码中检查递归,就必须在某处进行。 不幸的是,我无法更好地使用,因为我无法查看所有代码文件。

我得到相同的堆栈级别太深的错误&事实certificate问题是重复渲染部分。

我碰巧在主视图中调用渲染a_partial,然后在局部调用,我不小心再次调用了相同的局部。

HTH

由于您没有显示所有代码,我只能推测您已定义inspectto_s以构建包含父项等字符串的字符串。

您当前的parents方法似乎没有做任何合理的事情,因为无论您从哪个关联开始,它都会返回所有公司组织。 因此,任何公司都将自己作为母公司。 尝试将其转换为字符串将导致无限循环,以试图显示父母的父母的…

在任何情况下,你的parents方法的大部分都应该在一个帮助器中,称为options_for_parents_select ,因为它似乎正在做什么? 即使这样,第一个空选择也应该作为allow_null传递给select。

它设置实例变量的事实是代码气味。

祝好运

我找到了解决这个问题的方法……

我正在使用Rails 3,我的class级看起来像这样(问题的方法也是这样)

 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,或者从现在开始不支持它们并且之前抛出错误而不是警告

谢谢你的帮助Leo

如果您收到此错误,则表示您在应用程序中使用的rails版本与Ruby版本不兼容。

您可以使用解决方案来解决此问题。

1)您需要将ruby版本降级到旧版本。

2)或者您需要将Rails升级到最新版本。