Setting `cache_classes` to `false` fixes my bug. What to do next?

陌路散爱 提交于 2020-03-05 06:18:30

问题


I'm adding a mountable engine to my rails app, which provides a forum-like functionality (i.e. adds questions, answers, comments, etc). Everything works fine in development. In staging/production, however, I get an error when trying to create an answer, specifically a CanCan permission error (though I think that may be a red herring). But it feels like ti might be related to namespacing or name collision?

At any rate, the issue goes away when I set config.cache_classes = false in my environment configuration file. But that's not an acceptable solution since it really slows things down.

Where do I go from here? How can I figure out which classes needs to be reloaded? What's the conventional way to deal with this problem?

I'm using Rails 3.1.


回答1:


I figured out the problem. It was an issue with boot order. My Engine::AnswersController was being loaded before my Engine::ApplicationController. And in my code, I was inheriting from just ApplicationController. That means, since Engine::AnswerController didn't know about Engine::ApplicationController it was erroneously inheriting from the main app's ApplicationController rather than the engine's ApplicationController.

The fix was to force the answers controller to inherit from the engine rather than the main app by making the engine namespace explicit:

Engine::AnswersController < Engine::ApplicationController

I understand this problem is rather idiosyncratic to my application but how I arrived at the solution might be helpful to others. If you're faced with this type of problem, drop down into some type of debugger (I used Pry) and start inspecting things. Specifically, check what classes are loaded and look hard at the inheritance chain (i.e. Engine::ApplicationController.ancestors). Look for anything that seems odd.

Also if the issue is engine related, you might need to place debuggers in both the engine and the main app. Debugging engines can be difficult since errors occurring in the engine might not necessarily bubble up to the main app.

Much thanks to tjbladez for helping me figure this out.



来源:https://stackoverflow.com/questions/7905618/setting-cache-classes-to-false-fixes-my-bug-what-to-do-next

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!