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

标签 ruby-on-rails ruby

我在使用 Ruby 1.8.7 和 Rails 时遇到堆栈级别太深的错误 3.0.4 并使用 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

最佳答案

当您不小心递归地更改属性时,通常会发生此错误。如果您在用户模型中有一个用户名属性,以及一个名为用户名的虚拟属性,即直接更改用户名,您最终会调用虚拟,虚拟再次调用虚拟等等。因此,看看是否有什么东西就像在您的代码中某处发生的那样。

关于ruby-on-rails - Ruby on Rails 中的堆栈级别太深错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5446055/

相关文章:

ruby-on-rails - rails : comparison of Status with Status failed

ruby-on-rails - 使用 STI 时,如何强制 Rails 在 link_to 帮助程序中使用父类(super class)名称?

ruby-on-rails - ruby OAuth 噩梦 : Using Contacts API

ruby-on-rails - 发生错误,此迁移和所有后续迁移均已取消 [Rails DB 错误]

ruby - 如何在 ruby​​ 中按上次修改时间排序文件?

sql - rails : How to fetch records that are 2 'has_many' levels deep?

ruby-on-rails - 获取 FactoryBot::DuplicateDefinitionError:工厂已经注册

ruby-on-rails - Rails 3,如何添加一个不使用与应用程序其余部分相同布局的 View ?

ruby-on-rails - 如何在 en.yml 上格式化带有 “th” 后缀的日期?

ruby-on-rails - rails : Validating min and max length of a string but allowing it to be blank