Background: I’m using ActiveRecord to handle some bulk uploads. I
decided it would be a good idea to put all my models into subfolders,
then load them with autoload [essentially, so I can namespace
everything into a module]. Here’s the relevant code (i’m using
MODULENAMEBase.rb as the naming convention for the class that does
establish_connection):
module DBNamespace
def self.included(m)
@models_path = “models/#{m}”
require “#{@models_path}/#{m}Base.rb”
Dir.new(@models_path).entries.grep(/.rb$/).collect do |fn|
cname = fn.sub(/.rb$/, ‘’)
cname[0] = cname[0].chr.upcase
m.autoload(cname, “#{@models_path}/#{fn}”) unless fn ==
“#{m}Base.rb”
end
end
end
module PL
include DBNamespace
end
Right now, I can check PL.autoload?(:Agent) and get the correct path;
I can even load the constant with PL.const_get(:Agent). However,
trying PL::Agent gives me a:
/usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/
dependencies.rb:278:in load_missing_constant': uninitialized constant PL::Agent (NameError) from /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/ active_support/dependencies.rb:467:in
const_missing’
And doing an include PL; Agent gives me the odder:
/usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/
dependencies.rb:252:in load_missing_constant': Object is not missing constant Agent! (ArgumentError) from /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/ active_support/dependencies.rb:467:in
const_missing’
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/
active_support/dependencies.rb:479:in `const_missing’
from header.rb:7
Can anybody clear this up?