Nathan Fiedler
2007-Aug-06 17:40 UTC
''require'' in lib code not evaluated twice in development mode?
I have a model that does an ''include'' of a module defined in
the lib
directory. The module, in turn, uses ''require'' to load a class
defined
in the system load path. Running the application in development mode,
the initial request goes through successfully. However, the second
time around, I get a NameError resulting from "uninitialized
constant", with the constant in question being a class that is defined
in the script that is ''require''d in the lib module. In brief
it looks
like this:
app/model/revision.rb:
class Revision
include MyLib::Mod
end
lib/my_lib/mod.rb:
module MyLib
module Mod
require ''extension/clazz''
end
end
On the second request, the class defined in extension/clazz cannot be
found, resulting in the NameError. However, if I add "require
''extension/clazz''" into the model, just before the
''include'', it works
such that every request is successful. Likewise, if I run the
applicaiton in production mode, it works every time.
Is this expected behavior, or is this a bug in Rails?
Thanks
n
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Nathan Fiedler
2007-Aug-06 22:26 UTC
Re: ''require'' in lib code not evaluated twice in development mode?
Being new to Rails, it''s no surprise I didn''t understand what was happening. So the scripts in the lib directory are only evaluated once. While I knew that, I kinda forgot when faced with this problem. Given that in development mode Rails will de-initialize all the constants defined during the request (or something of that nature, again I''m new to Rails), it threw away the classes that were defined within the scripts in the lib directory. And given it never evaluated those scripts again, even in development mode, it didn''t reload the classes. However, a simple work-around of using ''require'' on the lib scripts made the problem go away. So the model contains the following (where the ''Mod'' module is defined in the script lib/my_lib/mod.rb): require ''my_lib/mod'' include MyLib::Mod I don''t think this is all that beautiful, but maybe someday I''ll learn the proper way to do this. n --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---