# A copy of "Module X" has been removed from the module tree b

**URL:** <https://www.ruby-forum.com/t/a-copy-of-module-x-has-been-removed-from-the-module-tree-b/109466>\
**Category:** Rails\
**Created:** [July 15, 2007, 1:12pm UTC](https://www.ruby-forum.com/t/a-copy-of-module-x-has-been-removed-from-the-module-tree-b/109466 "2007-07-15T13:12:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alvim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@alvim](https://www.ruby-forum.com/u/alvim)\
**Post date:** [July 15, 2007, 1:12pm UTC](https://www.ruby-forum.com/t/a-copy-of-module-x-has-been-removed-from-the-module-tree-b/109466/1 "2007-07-15T13:12:33Z")

</div>

Hi!

This will be a long one, sorry about that.

I’ve been playing with Ruby and Rails for the last year, but only  
getting a little more “serious” now. I like how one line in a model  
(“has\_many”, for example) will create several methods for you, and I  
had a need to do something similar myself, so I decided to give it a  
shot, looking at other people’s codes. Please bear in mind I am no  
expert in Ruby or Rails, but this is what I tried:

```auto
First, I include my model in ActiveRecord::Base on startup, by doing
this:
(/config/environment.rb, last line)
ActiveRecord::Base.send(:include, MyTest)

Then the module itself:
(/lib/my_test.rb)
module MyTest
  def self.included(base)
    base.extend(ClassMethods)
  end
  module ClassMethods
    def do_my_stuff
      module_eval do
        include MyTest::InstanceMethods
      end
    end
  end
  # I actually have different instance method modules, this is why I
went with this way
  module InstanceMethods
    def my_cool_method; "COOL!"; end
    def my_other_cool_method; "COOLER!"; end
  end
end

Finally, the actual model:
(/app/models/my_model.rb)
class MyModel < ActiveRecord::Base
  do_my_stuff # now every MyModel instance should have my_cool_method
end

Controller:
(/app/controllers/main_controller.rb)
class MainController < ApplicationController
  def index
    @mymodel = MyModel.find(:first)
    render :text => @mymodel.my_cool_method
  end
end
```

After all this, I run to [http://localhost:3000/main/index](http://localhost:3000/main/index)  
First run outputs “COOL!”, as expected

Second run:

A copy of MyTest::ClassMethods has been removed from the module tree  
but is still active!

I’ll spare you the full trace, but it begins like this:

C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active\_support/  
dependencies.rb:237:in `load_missing_constant' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:452:in `const\_missing’  
#{RAILS\_ROOT}/lib/my\_test.rb:9:in `do_my_stuff' #{RAILS_ROOT}/lib/my_test.rb:8:in `module\_eval’  
#{RAILS\_ROOT}/lib/my\_test.rb:8:in `do\_my\_stuff’  
#{RAILS\_ROOT}/app/models/my\_model.rb:8

Well, that’s about it. I’ve googled the message, been through several  
Trac tickets and forum postings, but all of them seem to be from  
before rails 1.2.3 (which is the version I’m using) and related to  
plugins who had controllers, and before\_filters, and the such. I don’t  
have any of those, so this is why I’m asking here.

I’m using Rails 1.2.3 and ruby 1.8.6 (2007-03-13 patchlevel 0) [i386- mswin32]

I appreciate any help you can give me with this.

Thanks,  
Marcelo A…

---

<div class="post-metadata">

**Author:** ![alvim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@alvim](https://www.ruby-forum.com/u/alvim)\
**Post date:** [July 17, 2007, 6:10am UTC](https://www.ruby-forum.com/t/a-copy-of-module-x-has-been-removed-from-the-module-tree-b/109466/2 "2007-07-17T06:10:51Z")

</div>

Well, I discovered what it was. The answer was somewhat buried in the  
Rails Trac,  
so I am going to post it here to help future people that have the same  
problem.

It has to do with Rails loading and unloading stuff in development  
mode. If you do a  
"require ‘lib/my\_test’ " in your environment.rb, it goes away.

Yes, stuff in the lib directory is supposed to be automatically  
loaded, thus eliminating  
the need for manual requires. And stuff in the lib directory IS, in  
fact, loaded. The reloading  
mechanism is the one that is kind of funny. Well, this does the trick.

See you guys,  
Marcelo A…
