If you are using a nested model and that model is using an association in that same namespace, AND you are in development mode wherein Dependencies.mechanism is set to :load for automatic reloading, const_missing will fail on the second load of an action calling that association. This is on edge rails.
This is easily illustrated by using acts_as_tree. Calling Resources::Category#children will raise this exception on the second load of an action or view making that call.
The basic flow goes like this:
In the first request, Resources::Category is called and const_missing loads the class properly with require_dependency. Resources (the module) is then called and const_missing defines a new module (line 115 in dependencies.rb) named Resources. Since Resources::Category was called first in this initial load, the problem is not apparent (and thus is also fine in production mode since Dependencies.mechanism is set to :require).
Then, in refreshing the action, the load order changes slightly (not sure why). Resources::Category is called and const_missing loads the class through require_dependency, then Resources (the module again) is called and const_missing again instantiates Resources as a new Module again (overridding what was previously loaded). Then Resources::Category is called again, but since const_missing redefined the Resources module, the previously loaded Resources::Category is no longer available and the NameError on line 106 is raised.
Coincidentally, Ruby does warn that the class is being redefined:
"./script/../config/../vendor/rails/activesupport/lib/active_support/dependencies.rb:115: warning: already initialized constant Resources"
Now, I seem to have solved the problem by preventing the module from being redefined if the class_id is defined (see attached patch).
Additionally, here is the debug output from Dependencies#const_missing that I used to trace the load order for the first and second requests, respectively:
Class ID: Resources, File Name: resources, File Path: resources
Class ID: CategoriesController, File Name: categories_controller, File Path: resources/categories_controller
Class ID: Category, File Name: category, File Path: resources/category
Class ID: Resources, File Name: resources, File Path: resources/resources
Class ID: Resources, File Name: resources, File Path: resources
Class ID: Resource, File Name: resource, File Path: resources/resource
Class ID: CategoriesController, File Name: categories_controller, File Path: resources/categories_controller
Class ID: Category, File Name: category, File Path: resources/category
Class ID: Resources, File Name: resources, File Path: resources/resources
Class ID: Resources, File Name: resources, File Path: resources
Class ID: Category, File Name: category, File Path: resources/category
Class ID: Category, File Name: category, File Path: resources/category
I tried for about 2 hours to write a functional test that could replicate these conditions, but did not succeed. Instead, I did write an app that can replicate the issue (also attached). Hopefully that is enough for someone more knowledgeable than I to write a proper test for this.