Ruby on Rails 中的堆栈级别太深错误 [英] Stack level too deep error in Ruby on Rails

查看:41
本文介绍了Ruby on Rails 中的堆栈级别太深错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Ruby 1.8.7 和 Rails 时遇到堆栈级别太深的错误3.0.4 并使用 rails 控制台执行以下命令.

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.

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

推荐答案

我已经找到了这个问题的解决方案...

I've found the solution to this issue...

我使用的是 Rails 3,我的类看起来像这样(有问题的方法也是这样)

I'm using Rails 3 and my class looks like this (and the problematic methods was this too)

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 有问题

I did have to hack a lot in the code to find out something was wrong with the named_scope on the line

@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.

我想知道在使用类方法时是否不推荐使用 named_scope 或者从现在开始不支持它们并抛出错误而不是之前的警告

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

感谢您的帮助狮子座

这篇关于Ruby on Rails 中的堆栈级别太深错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆