Forum: Ruby on Rails A copy of "Module X" has been removed from the module tree b

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
5ef2a067cc3a4f49021b6f781cabf7bb?d=identicon&s=25 alvim (Guest)
on 2007-07-15 13:12
(Received via mailing list)

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:

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

Then the module itself:
module MyTest
  def self.included(base)
  module ClassMethods
    def do_my_stuff
      module_eval do
        include MyTest::InstanceMethods
  # 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

Finally, the actual model:
class MyModel < ActiveRecord::Base
  do_my_stuff  # now every MyModel instance should have my_cool_method

class MainController < ApplicationController
  def index
    @mymodel = MyModel.find(:first)
    render :text => @mymodel.my_cool_method

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:

dependencies.rb:237:in `load_missing_constant'
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'

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-

I appreciate any help you can give me with this.

Marcelo Alvim.
5ef2a067cc3a4f49021b6f781cabf7bb?d=identicon&s=25 alvim (Guest)
on 2007-07-17 06:10
(Received via mailing list)
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

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.
This topic is locked and can not be replied to.