This project is archived and is in readonly mode.
Module attribute throws NameError on second request (but not first)
-
Frederick Cheung
In development mode your source is reloaded before every request (but the initializer isn't re-run) so you lose the fact that the value has been set.
-
Neeraj Singh
Frederick:
Since MyModule is defined in lib, I don't think Rails should unload it. Rails reloads only the constants found in app/controllers, app/models, app/helpers.
I know for sure that anything defined in lib is not reloaded.
So does that mean Rails is unloading the constant MyModule but is not re-loading it.
-
Frederick Cheung
Well in the brief experiment I did stuff from lib is reloaded between requests (at least by default).
-
tf
- Importance changed from to
Wrapping the initializer code in a config.to_prepare block might also solve the problem.
-
David Trasbo
I haven't actually tested this yet, but bear with me:
How can this
NameErrorhave anything to do with the fact thatlibfiles are reloaded, but initializers aren't? This should be the course of action as I see it:- The server is started.
lib/my_module.rbis loaded.- The initializer is run, setting the value.
- A page is requested.
- The value gets printed as intended.
- Another page is requested.
lib/my_module.rbis reloaded, thus loosing the value.- The initializer is not run.
MyModel.my_attributewill return nil, which interpolated into a string gives"".- The message should be printed without errors, but not with the value we intended.
Of course I'll look into this as soon as I get home, but that's just my thoughts at this moment.
-
Santiago Pastorino
- State changed from new to open
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino
- State changed from open to stale