新手问题 一个诡异的 "NameError: uninitialized constant" Bug

somejump · 2017年04月02日 · 最后由 lgn21st 回复于 2017年04月03日 · 6366 次阅读

我在服务器上用 RAILS_ENV=production rails console 结果出现如下报错:

Sport.last
NameError: uninitialized constant Sport
    from (irb):1
    from /usr/local/rvm/gems/ruby-2.2.2/gems/railties-5.0.2/lib/rails/commands/console.rb:65:in `start'
    from /usr/local/rvm/gems/ruby-2.2.2/gems/railties-5.0.2/lib/rails/commands/console_helper.rb:9:in `start'
    from /usr/local/rvm/gems/ruby-2.2.2/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:78:in `console'
User.new
NameError: uninitialized constant User

也就是 rails console 下所有的 model 都没有加载出来,但是奇怪的是 puma 服务器居然跑起来了。而且并没有出错,使用 Sport 类的 controller 里的方法都正常工作。

问题是。。。一天之前我进过 console,输入过这些简单的命令,那时根本没有出现这样的问题。。。

class Sport < ApplicationRecord
  validates :teamone,:status,:title,presence: true
  validates :wp,:lp, length:{maximum:30}
end
class User < ApplicationRecord
  has_secure_password
end

以上是 Model 代码,求大神解救。。。

如果用 RAILS_ENV=production bundle exec rails console 试试看呢?

lgn21st 回复

试过了,一样的结果,console 表示根本没不认识这些 Model

somejump 回复
puts $LOAD_PATH

检查一下当前 console 环境下的类载入路径是否包含了你要访问的 model 目录,我怀疑最近的修改或者系统环境设置改变了 $LOAD_PATH

lgn21st 回复
/home/sports_meeting/lib
/home/sports_meeting/vendor
/home/sports_meeting/app/assets
/home/sports_meeting/app/channels
/home/sports_meeting/app/controllers
/home/sports_meeting/app/controllers/concerns
/home/sports_meeting/app/helpers
/home/sports_meeting/app/jobs
/home/sports_meeting/app/mailers
/home/sports_meeting/app/models
/home/sports_meeting/app/models/concerns
/usr/local/rvm/gems/ruby-2.2.2/gems/turbolinks-5.0.1/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/jquery-rails-4.2.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/jquery-rails-4.2.2/vendor
/usr/local/rvm/gems/ruby-2.2.2/gems/coffee-rails-4.2.1/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/actioncable-5.0.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/bundler-1.14.3/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/spring-2.0.1/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/web-console-3.4.0/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/uglifier-3.1.9/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/turbolinks-source-5.0.0/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/sqlite3-1.3.13/lib
/usr/local/rvm/gems/ruby-2.2.2/extensions/x86_64-linux/2.2.0/sqlite3-1.3.13
/usr/local/rvm/gems/ruby-2.2.2/gems/spring-watcher-listen-2.0.1/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/sass-rails-5.0.6/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/tilt-2.0.7/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/sass-3.4.23/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/rails-5.0.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/sprockets-rails-3.2.0/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/sprockets-3.7.1/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/puma-3.8.2/lib
/usr/local/rvm/gems/ruby-2.2.2/extensions/x86_64-linux/2.2.0/puma-3.8.2
/usr/local/rvm/gems/ruby-2.2.2/gems/listen-3.0.8/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/rb-inotify-0.9.8/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/rb-fsevent-0.9.8/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/jbuilder-2.6.3/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/multi_json-1.12.1/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/ffi-1.9.18/lib
/usr/local/rvm/gems/ruby-2.2.2/extensions/x86_64-linux/2.2.0/ffi-1.9.18
/usr/local/rvm/gems/ruby-2.2.2/gems/debug_inspector-0.0.2/lib
/usr/local/rvm/gems/ruby-2.2.2/extensions/x86_64-linux/2.2.0/debug_inspector-0.0.2
/usr/local/rvm/gems/ruby-2.2.2/gems/railties-5.0.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/thor-0.19.4/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/method_source-0.8.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/coffee-script-2.4.1/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/execjs-2.7.0/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/coffee-script-source-1.12.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/byebug-9.0.6/lib
/usr/local/rvm/gems/ruby-2.2.2/extensions/x86_64-linux/2.2.0/byebug-9.0.6
/usr/local/rvm/gems/ruby-2.2.2/gems/bcrypt-3.1.11/lib
/usr/local/rvm/gems/ruby-2.2.2/extensions/x86_64-linux/2.2.0/bcrypt-3.1.11
/usr/local/rvm/gems/ruby-2.2.2/gems/activerecord-5.0.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/arel-7.1.4/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/activemodel-5.0.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/actionmailer-5.0.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/mail-2.6.4/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/mime-types-3.1/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/mime-types-data-3.2016.0521/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/activejob-5.0.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/globalid-0.3.7/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/websocket-driver-0.6.5/lib
/usr/local/rvm/gems/ruby-2.2.2/extensions/x86_64-linux/2.2.0/websocket-driver-0.6.5
/usr/local/rvm/gems/ruby-2.2.2/gems/websocket-extensions-0.1.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/nio4r-2.0.0/lib
/usr/local/rvm/gems/ruby-2.2.2/extensions/x86_64-linux/2.2.0/nio4r-2.0.0
/usr/local/rvm/gems/ruby-2.2.2/gems/actionpack-5.0.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/rack-test-0.6.3/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/rack-2.0.1/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/actionview-5.0.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/rails-html-sanitizer-1.0.3/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/loofah-2.0.3/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/rails-dom-testing-2.0.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/nokogiri-1.7.1/lib
/usr/local/rvm/gems/ruby-2.2.2/extensions/x86_64-linux/2.2.0/nokogiri-1.7.1
/usr/local/rvm/gems/ruby-2.2.2/gems/mini_portile2-2.1.0/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/erubis-2.7.0/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/builder-3.2.3/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/activesupport-5.0.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/tzinfo-1.2.2/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/thread_safe-0.3.6/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/minitest-5.10.1/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/i18n-0.8.1/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/concurrent-ruby-1.0.5/lib
/usr/local/rvm/gems/ruby-2.2.2/gems/rake-12.0.0/lib
/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0
/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/x86_64-linux
/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby
/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/vendor_ruby/2.2.0
/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/vendor_ruby/2.2.0/x86_64-linux
/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/vendor_ruby
/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0
/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/x86_64-linux

这个是 LOAD_PATH...看起来没什么不对的....

somejump 回复

看不出来问题,灵异的状况。

如果你手动 require 'sport'会发生什么呢?

lgn21st 回复
require "sport"
NameError: uninitialized constant ApplicationRecord
        from /home/sports_meeting/app/models/sport.rb:1:in `<top (required

这回更奇怪。。。

somejump 回复

我已经开始怀疑你的文件系统故障了。

lgn21st 回复

还好我服务器有备份。。。那我换个服务器?

somejump 回复

祝你好运,说不定重启一下就好了。

lgn21st 回复

大神你说的太对了,重启好了

somejump 回复

我蒙的啊!

可以试试 bin/spring stop 的 这种情况可能是 spring 搞得鬼

spring 的原因。

jasl 回复

很有道理,我记得 spring 默认只会在本地 development 环境下启用。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册