Reputation: 6769
I recently got bit by "A copy of Klass has been removed from the module tree but is still active", and http://makandra.com/notes/528-fixing-a-copy-of-klass-has-been-removed-from-the-module-tree-but-is-still-active completely addressed my problem. I have a module being included from two ActiveRecord classes which calls a static MyModel.find method.
I understand that I can get around my problem in a few different ways, but I'm wondering, what exactly is Ruby doing underneath the hood that makes calling a static method in this way a bad idea?
Upvotes: 2
Views: 400
Reputation: 211670
The models themselves are rebuilt each time a request is made when the application is in development mode, so any lingering references to the old classes or instances of them are rendered invalid and will not work properly, if at all.
It can be tricky to get things to auto-reload properly, but it can be done. It just depends on what you're trying to do.
As nearly everything in the app/
structure gets reloaded automatically, you may be able to side-step this issue by having your extension get reloaded too. Things in lib/
do not get reloaded and may cause conflict.
I think the primary issue here is that the extension in lib/
will resolve the class and retain that reference even when the class gets replaced on a subsequent load.
If you need to do something like this that can't be fixed by moving your extension into app/models
, can you provide more details on what it is you're trying to do?
Upvotes: 1