This project is archived and is in readonly mode.
Module attribute throws NameError on second request (but not first)
Reported by Koo Koolen | May 5th, 2009 @ 08:58 PM | in 3.x
Hello,
I have this odd error of a module attribute throwing a NameError on the second request but not on the first.
Here are the directions to reproduce it:
rails module_attribute_issue
cd module_attribute_issue
# In lib/my_module.rb
module MyModule
# I could have used "mattr_accessor :my_attribute"
# but it would not raise NameError and return nil for my_attribute instead
def self.my_attribute
@@my_attribute
end
def self.my_attribute=(value)
@@my_attribute = value
end
end
# In config/initializers/my_app_initializer.rb
MyModule.my_attribute = "Some value"
# In app/controllers/application.rb
class ApplicationController < ActionController::Base
before_filter do
puts "DEBUG : MyModule.my_attribute = #{MyModule.my_attribute}"
end
end
./script/generate controller Home index
./script/server
Then I fire my browser at http://localhost:3000/home/index and get the page back, fine. But when I try it a second time, I get the NameError
Thanks !
Comments and changes to this ticket
-
Frederick Cheung May 11th, 2009 @ 02:24 PM
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 May 11th, 2009 @ 04:07 PM
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 May 11th, 2009 @ 04:19 PM
Well in the brief experiment I did stuff from lib is reloaded between requests (at least by default).
-
tf September 21st, 2010 @ 11:35 PM
- Importance changed from to
Wrapping the initializer code in a config.to_prepare block might also solve the problem.
-
David Trasbo September 23rd, 2010 @ 09:15 AM
I haven't actually tested this yet, but bear with me:
How can this
NameError
have anything to do with the fact thatlib
files are reloaded, but initializers aren't? This should be the course of action as I see it:- The server is started.
lib/my_module.rb
is 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.rb
is reloaded, thus loosing the value.- The initializer is not run.
MyModel.my_attribute
will 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 February 2nd, 2011 @ 04:31 PM
- 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 February 2nd, 2011 @ 04:31 PM
- State changed from open to stale
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>