I have two classes in rails 3.2.6:
- class Foo in models/foo.rb (is in table foos, per default)
- class Bar::Foo in models/bar/foo.rb (which sets
self.table_nametobar_foos)
When I go into console, I get:
> Bar::Foo
=> Bar::Foo(id: ...)
> Foo # or ::Foo
LoadError: expected models/bar/foo.rb to define Foo
What’s wrong?
We solved this in IRC, but the core issue is that there was a
config.autoload_pathsglob set that was includingmodels/**as load paths.Rails’ auto-loader iterates the load paths, and tacks on the constant name. Once it finds a file that exists, it tries to load it, then throws an exception if the constant is not available.
So, what was happening is Rails had a list of load paths like:
It was iterating the paths, and would find a match at
/models/bar/foo.rb, which it then loads (which makesBar::Fooavailable, but notFoo), then throws the exception becauseFooisn’t available.The solution in this case was to remove the
autoload_pathssetting, so that Rails would not find the wrong file to load for the root-level constant.