If I create a directory called jobs at /app/jobs
then add a ruby class Jobs::Bar (/app/jobs/bar.rb)
instantiating this object while running under 'rails server' fails
with "NameError: uninitialized constant Jobs"
and in 'rails console' it fails with the same error but can then be
loaded (see below console session)
using rvm
running ruby-1.9.1-p378 [ i386 ]
Rails 3.0.0.beta3
the below works fine in rails 2.2.2 and ruby 1.8.7
# /config/application.rb
config.load_paths += %W( #{config.root}/app/jobs )
# /app/jobs/bar.rb
module Jobs
class Bar
def hello
"Bar says hello"
end
end
end
$> rails console
ruby-1.9.1-p378 > b = ::Jobs::Bar.new
NameError: uninitialized constant Jobs
from (irb):3
from /Users/house/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta3/lib/rails/commands/console.rb:47:in `start'
from /Users/house/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta3/lib/rails/commands/console.rb:8:in `start'
from /Users/house/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta3/lib/rails/commands.rb:34:in `<top (required)>'
from script/rails:9:in `require'
from script/rails:9:in `<main>'
# it makes sense that this would fail since it doesn't really exist
ruby-1.9.1-p378 > b = Bar.new
LoadError: Expected /Users/house/projects/sandboxfor3/app/jobs/bar.rb to define Bar
from /Users/house/.rvm/gems/ruby-1.9.1-p378/gems/activesupport-3.0.0.beta3/lib/active_support/dependencies.rb:453:in `load_missing_constant'
from /Users/house/.rvm/gems/ruby-1.9.1-p378/gems/activesupport-3.0.0.beta3/lib/active_support/dependencies.rb:153:in `block in const_missing'
from /Users/house/.rvm/gems/ruby-1.9.1-p378/gems/activesupport-3.0.0.beta3/lib/active_support/dependencies.rb:151:in `each'
from /Users/house/.rvm/gems/ruby-1.9.1-p378/gems/activesupport-3.0.0.beta3/lib/active_support/dependencies.rb:151:in `const_missing'
from (irb):4
from /Users/house/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta3/lib/rails/commands/console.rb:47:in `start'
from /Users/house/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta3/lib/rails/commands/console.rb:8:in `start'
from /Users/house/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta3/lib/rails/commands.rb:34:in `<top (required)>'
from script/rails:9:in `require'
from script/rails:9:in `<main>'
# now it works as expected? but only after doing 'b = Bar.new' which is invalid?
ruby-1.9.1-p378 > b = ::Jobs::Bar.new
=> #<Jobs::Bar:0x2484b64>
ruby-1.9.1-p378 > b.hello
=> "Bar says hello"
Everything works correctly in the following similar
situations
- namespaces under /app/models, -- for example
/app/models/jobs/bar.rb with the exact same class definition and no
config.load_paths
- a non-namespaced class under /app/jobs with config.load_paths
-- for example
# /app/jobs/foo.rb
class Foo
end
Foo.new # no errors