I have created some classes in a module, f.i. Request (app/models/api/request.rb)
class Api::Request
end
And now I want to check in the console does the class exist?
>> Api.const_defined?('Request')
=> false
>> Api::Request
=> Api::Request
>> Api.const_defined?('Request')
=> true
Strange, but the script cannot see the class name unless I use it in the code.
How to fix this? And if you can explain the issue, it will be great.
The project uses Rails 2.3
The problem here is with the rails autoload mechanism. Basically what it does is to react to the method const_missing which you can define on every object. When this method is called, Rails looks for a file which is likely to contain a definition for the missing constant and requires it. The problem is that
triggers const_missing but
does not.
You can write your own const_defined? like this (the code would go into a rails initializer):
which first tries to autoload the constant.