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:
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
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…