-
Notifications
You must be signed in to change notification settings - Fork 21.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autoloader confuses constants #14844
Comments
The problem is that This is why I get the warning
I can't think of any quick work-around for that. |
Exactly.
A thing you can do there is for example require_dependency 'comment/form' at the bottom of comment.rb. That way comment/form.rb is going to be evaluated, AS treats it as it had been autoloaded, and Ruby will find Alternatively you can loop and do that for all files in the comment directory or whatever fits better the particular case of that application. But that would be the basic idea of a workaround. |
Closing by now, since it is a consequence of how Ruby works. Please feel free to reopen or comment if there's anything else to add. |
Thanks @fxn - I figured it's the way of the autoloader's "passive" technique of loading constants (i.e. waiting for a constant to be missing. As a workaround, I do class Comment < ..
autoload :Form, 'comment/form'
end I am actually thinking about an alternative approach to an autoloader, a more explicit way where we use Ruby's |
Good! I explored the |
Ok- does that mean using Yeah, it would be awesome if autoload accepted paths, like autoload "Comment::Form", 'comment/form' |
Exactly, this feature is not based on That line wouldn't work, |
Yeah, I was just thinking (dreaming?) aloud 😁 Is there a way to hook into |
Nope, it is designed to work transparently and triggers no event. |
Thanks for pointing me to |
Exactly. We need a guide about that topic :(. |
A guide as in guides.rubyonrails.org/ or would a blog post be sufficient? Where are the source for guides? |
A guide in guides.rubyonrails.org: constant autoloading is basically undocumented but it is a key feature users need to understand. The source code for guides lives in the guides directory under the project root. If I find the time that's something I'd like to work on after RailsConf. |
The autoloader has a problem with pre-loaded constants that are later found in namespaces (completely unrelated).
This file is simulating a gem.
Let's assume the file
lib/form.rb
is required in an initializer (NOT autoloaded). This usually happens with gem files, too. The constant::Form
is now defined.Next, I create a model.
As a last step, I add a
Form
class into theComment
namespace.Provoking the bug is as simple as calling
in a controller, console, etc.
It throws
Apparently, the autoloader references the top-level
::Form
module where should be the namespacedComment::Form
constant.It looks as if
comment/form.rb
isn't even loaded.This bug exists in Rails 3.1, 3.2, 4.0 and 4.1.
The text was updated successfully, but these errors were encountered: