Hello, I got the error "The stack level is too deep" and I am sure that it is generated from this model. I know this has something to do with a recursive call, but I haven’t been able to find it yet, thanks.
class Character < ActiveRecord::Base
belongs_to :user
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
[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 - the stack level is too deep:
activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:85:in `'