Rails无法自动加载常量

时间:2015-03-13 16:01:33

标签: ruby-on-rails ruby activerecord

我有以下文件夹结构:

- models/
  - mappings/
    - user.rb
  - mapping.rb
  - service.rb
  - user.rb
  - ...

在mappings / user.rb中,我有以下内容:

module Mappings
  class User < Mapping

  end
end

而mapping.rb是:

class Mapping < ActiveRecord::Base

end

在config / application.rb中,我按如下方式自动加载模型:

config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/presenters)

很遗憾,我收到此load_missing_constant错误:

/home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:481:in `load_missing_constant': Unable to autoload constant User, expected /vagrant/app/models/mappings/user.rb to define it (LoadError)

非常感谢任何帮助。我不确定我错过了什么!

更新:

完整追踪:

  

16:53:48 jobs.1 |从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:180:in const_missing' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:238:in const_get&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:238:in   block in constantize' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:236:in 各&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:236:in   inject' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:236:in constantize&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:552:in get' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:583:in constantize&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise.rb:302:in   get' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:80:in ,以&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:75:in   modules' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:92:in 路由&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:159:in   default_used_route' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:69:in 初始化&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise.rb:336:in   new' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise.rb:336:in add_mapping&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/rails/routes.rb:224:in   block in devise_for' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/rails/routes.rb:223:in 各&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/rails/routes.rb:223:in   devise_for' 16:53:48 jobs.1 | from /vagrant/config/routes.rb:93:in 阻止&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.9/lib/action_dispatch/routing/route_set.rb:344:in   instance_exec' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.9/lib/action_dispatch/routing/route_set.rb:344:in eval_block&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.9/lib/action_dispatch/routing/route_set.rb:322:in   draw' 16:53:48 jobs.1 | from /vagrant/config/routes.rb:1:in&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:241:in load' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:241:in阻止加载&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:232:in load_dependency' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:241:in load&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:40:in block in load_paths' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:40:in每个&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:40:in load_paths' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:16:in重新加载!&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:26:in block in updater' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/file_update_checker.rb:75:in 来电&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/file_update_checker.rb:75:in   execute' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:27:in更新&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:7:in   execute_if_updated' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/finisher.rb:71:in 阻止&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/initializable.rb:30:in   instance_exec' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/initializable.rb:30:in 运行&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/initializable.rb:55:in   tsort_each中的block in run_initializers' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:226:in 阻止&#39; 16:53:48就业1从   /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:348:in   block (2 levels) in each_strongly_connected_component' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:427:in each_strongly_connected_component_from&#39; 16:53:48就业1从   /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:347:in   block in each_strongly_connected_component' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:345:in 各&#39; 16:53:48就业1从   /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:345:in   call' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:345:in each_strongly_connected_component&#39; 16:53:48就业1从   /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:224:in   tsort_each' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:205:in tsort_each&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/initializable.rb:54:in   run_initializers' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application.rb:300:in 初始化&#39!; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/railtie.rb:194:in   public_send' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/railtie.rb:194:in 的method_missing&#39; 16:53:48就业1从   /vagrant/config/environment.rb:8:in <top (required)>' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:247:in要求&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:247:in block in require' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:232:in load_dependency&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:247:in require' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application.rb:276:in require_environment!&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/runner.rb:52:in   <top (required)>' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:128:in需要&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:128:in require_command!' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:95:in runner&#39; 16:53:48就业1从   /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:40:in   run_command!' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands.rb:17:in &#39; 16:53:48就业1来自bin / rails:8:require' 16:53:48 jobs.1 | from bin/rails:8:in&#39;

所以看起来问题是因为我必须使用名为User的类,即使其中一个在模块内被命名空间。

1 个答案:

答案 0 :(得分:0)

我相信它是因为您在User模块中定义了Mapping类。它查看了user.rb,没有看到User类和错误。尝试删除模块调用,看看是否有效。

编辑:为了回应评论,或许您应该在类名中明确表示。 Mappings::User。您可能需要相应地继承它。