# 'require' in lib code not evaluated twice in development mod

**URL:** <https://www.ruby-forum.com/t/require-in-lib-code-not-evaluated-twice-in-development-mod/111388>\
**Category:** Rails\
**Created:** [August 6, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/require-in-lib-code-not-evaluated-twice-in-development-mod/111388 "2007-08-06T19:40:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![atraeyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@atraeyu](https://www.ruby-forum.com/u/atraeyu)\
**Post date:** [August 6, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/require-in-lib-code-not-evaluated-twice-in-development-mod/111388/1 "2007-08-06T19:40:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![atraeyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@atraeyu](https://www.ruby-forum.com/u/atraeyu)\
**Post date:** [August 7, 2007, 12:27am UTC](https://www.ruby-forum.com/t/require-in-lib-code-not-evaluated-twice-in-development-mod/111388/2 "2007-08-07T00:27:30Z")

</div>

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
