Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

config.load_paths and namespace issue

#4741

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

Reported by Jesse House · May 31st, 2010 @ 11:44 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:51 PM

  2. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:52 PM