Rails console in production: NameError: uninitialized constant
Asked Answered
S

2

34

I have a rails app (rails 5). In development, everything work, when i use

rails console

And enter an instruction, for example User.all , it's working.

In production, my app work perfectly, no problems, no errors, but when i use rails console production and enter for example User.all i have an error :

NameError: uninitialized constant User
    from (irb):2
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `block in require'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
    from /home/alexandre/tcheen/bin/rails:9:in `<top (required)>'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:in `load'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:in `call'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/command_wrapper.rb:38:in `call'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:191:in `block in serve'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `fork'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `serve'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

I have the same problem with all my classes, but i repeat, the application work perfectly. I develop on Mac OS and the app run in production on debian 8. My models are correctly named i verified. Thanks

Swallowtailed answered 11/9, 2016 at 16:12 Comment(3)
Does it work if you type ::User.all?Gilliette
Thanks but it work after a new ssh sessionSwallowtailed
Having this same issue suddenly. No idea what happenedArguseyed
A
106

I had this problem and realized it happened after I made a tweak to one of my job files. What fixed it was restarting the spring loader. Just run

spring stop

Then the next time you run the rails console it should load things as normal.

Arguseyed answered 7/11, 2016 at 3:18 Comment(6)
You are such a life-saver! I was stuck on this issue since more than 3 weeks. You really saved it for me :)Intercourse
@JagjotSingh sorry you didn't see it sooner! It also confused me for a long time! Glad it helped you now though!Arguseyed
Lost a couple of gray hairs over this one. Thanks.Mountebank
this was bugging me for a while, but mine was really random, like, come back in 1min and it'll be fixed random, but today I really needed it asap, appreciate it.. a couple of solutions in the long term: 1- disable it on production github.com/rails/spring#temporarily-disabling-spring or use the groups of bundle bundler.io/v1.12/groups.html. I was so, so sure that it would overwrite the Gemfile.lock, but apparently I'm just an ass(I told that too many times xd)Sulk
YOU ARE A LIFE-SAVER MAN!! YOU ARE!Stet
fwiw spring stop did not work for me, yet I did have a spring process via ps -ef | grep spring. After killing that process, the console loaded as mentioned.Procne
R
3

I had this same issue, rewolf's answer above solved it for me temporarily.

Just to add to his answer:

After stopping the spring gem by running the command below

spring stop

You can also fix this issue permanently by upspringing (removing the spring gem from) your bin/ executables:

bin/spring binstub --remove --all

Or

spring binstub --remove --all

You can now run the command below to get into rails console in production

rails c --environment=production

Also, to avoid this from occurring in subsequent occasions endeavour to make sure that the spring gem is only present in development and test groups in your Gemfile.

Moreso, when you're in production make sure you always provide the --without development test argument to the bundle install command

bundle install --without development test

and not the usual or common

bundle install

Please note: As an indication, whenever you run the command rails c or rails console and you see the output below:

Running via Spring preloader in process 26651 WARNING: Spring is running in production. To fix this make sure the spring gem is only present in development and test groups in your Gemfile and make sure you always use bundle install --without development test in production

It's an indication that the spring gem is running in your production environment, and it should be stopped or removed entirely from your bin executables.

That's all.

I hope this helps

Radioelement answered 30/1, 2020 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.