扶手:"堆栈层次过深"调用和当错误QUOT; ID"主键的方法 [英] Rails: "Stack level too deep" error when calling "id" primary key method
问题描述
这是<一个一个重新发布href="http://stackoverflow.com/questions/4130829/each-map-on-array-results-in-you-have-a-nil-object-when-you-didnt-expect-it">another发行,更好地孤立这个时候。 在我的environment.rb文件中我改变了这一行:
This is a repost on another issue, better isolated this time. In my environment.rb file I changed this line:
config.time_zone = 'UTC'
这一行:
config.active_record.default_timezone = :utc
于是乎,这个调用:
Ever since, this call:
Category.find(1).subcategories.map(&:id)
这是运行在开发环境中的第二次失败后的堆栈级别太深错误时config.cache_classes = FALSE。如果config.cache_classes =真,不会出现问题。 该错误是下面的code在active_record / attribute_methods.rb围绕线252结果:
Fails on "Stack level too deep" error after the second time it is run in the development environment when config.cache_classes = false. If config.cache_classes = true, the problem does not occur. The error is a result of the following code in active_record/attribute_methods.rb around line 252:
def method_missing(method_id, *args, &block)
...
if self.class.primary_key.to_s == method_name
id
....
要在ID功能重新呼叫的呼叫使用method_missing并没有什么prevents的ID被称为一遍又一遍,造成堆栈层次过深。
The call to the "id" function re-calls method_missing and there is nothing that prevents the id to be called over and over again, resulting in stack level too deep.
我用Rails 2.3.8。 该分类模型的has_many:子类别。 该行上的上述变体的呼叫失败(例如Category.first.subcategory_ids,使用每个而不是地图,等)。
I'm using Rails 2.3.8. The Category model has_many :subcategories. The call fails on variants of that line above (e.g. Category.first.subcategory_ids, use of "each" instead of "map", etc.).
有什么想法就会有强烈的AP preciated。
Any thoughts will be highly appreciated.
谢谢! 阿米特
推荐答案
- 这个答案是从我原来的职位复制<一href="http://stackoverflow.com/questions/4130829/each-map-on-array-results-in-you-have-a-nil-object-when-you-didnt-expect-it">here.
-- This answer is copied from my original post here.
终于解决了! 发布后第三个问题并用的trptcolin ,我可以确认工作的解决方案。
Finally solved! After posting a third question and with help of trptcolin, I could confirm a working solution.
问题:我用要求
来包括模型从表少模型(类是在app /模型,但不延长的ActiveRecord :: Base的)之内。例如,我有一个类 FilterCategory
的实施需要类别
。这搞砸了Rails的类缓存。
我不得不使用要求
摆在首位,因为线,如 Category.find:所有
失败
The problem: I was using require
to include models from within Table-less models (classes that are in app/models but do not extend ActiveRecord::Base). For example, I had a class FilterCategory
that performed require 'category'
. This messed up with Rails' class caching.
I had to use require
in the first place since lines such as Category.find :all
failed.
解决方案(归功于trptcolin):替换 Category.find:所有
与 :: Category.find:所有
。这工作,而不需要明确要求的任何模型,并且因此不会导致任何类缓存问题
The solution (credit goes to trptcolin): replace Category.find :all
with ::Category.find :all
. This works without the need to explicitly require any model, and therefore doesn't cause any class caching problems.
协议栈太深的问题也消失在使用 config.active_record.default_timezone =:UTC
The "stack too deep" problem also goes away when using config.active_record.default_timezone = :utc
这篇关于扶手:&QUOT;堆栈层次过深&QUOT;调用和当错误QUOT; ID&QUOT;主键的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!