簡體   English   中英

Ruby on Rails 中的堆棧級別太深錯誤

[英]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

由於您沒有顯示所有代碼,我只能推測您已經定義了inspectto_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM