A copy of "Module X" has been removed from the module tree but is still active!

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:

[code] 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 [/code]

After all this, I run to 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 Alvim.

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 Alvim.