简体   繁体   English

堆栈级别太深

[英]Stack level too deep

Hello I am received a "Stack level too deep" error and I am fairly sure that it is being generated from this model. 您好我收到了“Stack level too deep”错误,我很确定它是从这个模型生成的。 I know it has something to do with a recursive call, but I have thus far been unable to locate it, thanks. 我知道它与递归调用有关,但到目前为止我无法找到它,谢谢。

class Character < ActiveRecord::Base

  # Associations
  belongs_to :user

  # Validations
  validates :name, :presence => true, :uniqueness => true, :length => 
  { minimum: 2, maximum: 20 }, format: { with: /\A[a-zA-Z]+\Z/ }
  validates :race, :presence => true
  validates :class, :presence => true
  validates :user, :presence => true

  def self.races
    ["Human", "Dwarf", "Elven", "Orc", "Undead", "Demon"]
  end

  def self.classes
    {
      :fighter => {strength: 4, endurance: 3, dexterity: -2, charisma: -2, wisdom: -2, intelligence: -3}, 
      :thief   => {strength: -3,endurance: 2, dexterity: 4, charisma: 2, wisdom: -2, intelligence: 0}, 
      :magi    => {strength: -3, endurance: -2, dexterity: -2, charisma: 2, wisdom: 3, intelligence: -3}, 
      :ranger  => {strength: -2, endurance: 2, dexterity: 2, charisma: 0, wisdom: -3, intelligence: 0}, 
      :cleric  => {strength: 2,endurance: 2, dexterity: -3, charisma: -2, wisdom: 3, intelligence: 2}
    }
  end

  def set_class(_class)
    _attributes = Character.classes[_class.downcase]
    transaction do
      self.class = _class.downcase
      _attributes.each do |name, value|
        self.name += value
      end
      self.save
    end
  end
end

Server log: 服务器日志:

Started GET "/characters/new" for 127.0.0.1 at 2014-04-04 01:54:14 +0200
  [1m[36mActiveRecord::SchemaMigration Load (0.8ms)[0m  [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
Processing by CharactersController#new as HTML
  [1m[35mUser Load (1.5ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
  Rendered shared/_error_messages.html.erb (3.8ms)
  Rendered characters/new.html.erb within layouts/application (38.1ms)
Completed 500 Internal Server Error in 199ms

SystemStackError - stack level too deep: SystemStackError - 堆栈级别太深:

activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:85:in `'

There are three problems with your code: 您的代码有三个问题:

  1. you are using class as an attribute, 你正在使用class作为属性,
  2. you are confusing :symbols and "String".downcase , 你很困惑:symbols"String".downcase
  3. you are using self.name (attribute) thinking it's a different setter. 你正在使用self.name (属性)认为它是一个不同的setter。

Class

Don't use class . 不要class It's a reserved keyword. 这是一个保留的关键字。
These lines will leave ruby very confused: 这些线条会让ruby非常困惑:

validates :class, :presence => true

self.class = _class.downcase

Also, I assume that you have a class column in your DB table. 另外,我假设您的数据库表中有一个class列。 You should really rename it to something like character_class or rpg_class . 您应该将其重命名为character_classrpg_class

Symbols and Strings 符号和字符串

Your self.classes method returns a Hash whose keys are symbols. 您的self.classes方法返回一个Hash其键是符号。 And yet, later you try to access it using a string: 然而,稍后您尝试使用字符串访问它:

_attributes = Character.classes[_class.downcase]

While it's true that :foobar.downcase will do nothing, you can't access those values with "Foobar".downcase . 虽然确实如此:foobar.downcase什么都不做,你不能用"Foobar".downcase访问这些值。
To fix that, you could: 要解决这个问题,您可以:

In any case, that method could be improved by using memoization . 无论如何,可以通过使用memoization来改进该方法。
At the moment you are re-creating a hash of hashes with each call. 目前,您正在为每次调用重新创建哈希哈希值。 Instead, you can save it in a variable and create it only the first time. 相反,您可以将其保存在变量中,并仅在第一次创建它。

def self.classes
  @char_classes ||= { fighter: { #...
end

Or, even better, use a constant: 或者,更好的是,使用常量:

CHAR_CLASSES = { #.....

def self.classes
  CHAR_CLASSES
end

name 名称

I see that you have this validation in place: 我看到你已经有了这个验证:

validates :name, :presence => true, :uniqueness => true, :length => { minimum: 2, maximum: 20 }, format: { with: /\A[a-zA-Z]+\Z/ }

which means you have a name column in your DB table, and that it's supposed to be a String matching a specific format (only letters, between 2 and 20). 这意味着你的数据库表中有一个name列,它应该是一个匹配特定格式的String (只有2到20之间的字母)。

With this said, let's have a look at this block of code: 有了这个,让我们来看看这段代码:

_attributes.each do |name, value|
  self.name += value
end

In this local scope, name is a variable holding a Symbol (eg :strength ) and value is a Fixnum . 在此局部范围中, name是一个包含Symbol的变量(例如:strength ), value是一个Fixnum
When you do self.name += value , however, you are assigning the Fixnum to the object attribute name . 但是,当您执行self.name += value时,您将Fixnum分配给对象属性 name

I can't see where you define methods like strength= or dexterity= . 我看不到你在哪里定义像strength=dexterity= I'm assuming they are columns on the table. 我假设他们是桌上的专栏。

In that case, then this should work: 在那种情况下,这应该工作:

self.public_send("#{name}=", value)
# or just 'send' if it's a private method

When you are setting the attributes on your character, you need to generate the setter dynamically. 在角色上设置属性时,需要动态生成setter。 You can't call a dynamic method by appending a variable. 您不能通过附加变量来调用动态方法。 name = 'mike'; self.name name = 'mike'; self.name will not call self.mike . name = 'mike'; self.name不会调用self.mike

def set_class(_class)
  _attributes = Character.classes[_class.downcase]
  transaction do
    self.class = _class.downcase
    _attributes.each do |name, value|
      self.public_send("#{name}=", value)
    end
    self.save
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM