This is a repost on another issue, better isolated this time.
In my environment.rb file I changed this line:
config.time_zone = 'UTC'
to this line:
config.active_record.default_timezone = :utc
Ever since, this call:
Category.find(1).subcategories.map(&:id)
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
....
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.
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.).
Any thoughts will be highly appreciated.
Thanks!
Amit
— This answer is copied from my original post here.
Finally solved!
After posting a third question and with help of trptcolin, I could confirm a working solution.
The problem: I was using
requireto include models from within Table-less models (classes that are in app/models but do not extend ActiveRecord::Base). For example, I had a classFilterCategorythat performedrequire 'category'. This messed up with Rails’ class caching.I had to use
requirein the first place since lines such asCategory.find :allfailed.The solution (credit goes to trptcolin): replace
Category.find :allwith::Category.find :all. This works without the need to explicitly require any model, and therefore doesn’t cause any class caching problems.The “stack too deep” problem also goes away when using
config.active_record.default_timezone = :utc