[console in rails]. uninitialized constant Class - ruby-on-rails

when i run a method in terminal of rails.
first time, it working :
Spree::Campaign.first
Campaign Load (0.4ms) SELECT `campaigns`.* FROM `campaigns` ORDER BY `campaigns`.`id` ASC LIMIT 1
=> #<Campaign id: 1, name: "campaign 1", user_id: 1, created_at: "2015-10-27 06:48:01", updated_at: "2015-10-29 04:22:03", description: nil, active: true>
but when i try run code above again
Spree::Campaign.first
NameError: uninitialized constant Spree::Campaign
from (irb):2
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands/console.rb:90:in `start'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands/console.rb:9:in `start'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands.rb:17:in `<top (required)>'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
from /home/kop/rails/donghoxteen/bin/rails:8:in `<top (required)>'
from /home/kop/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/kop/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
Why? and how to fix this error??

The errors you are getting seem to be intermittent. In your question text, the error is uninitialized constant Spree::Campaign, whereas in your question title, the error is uninitialized constant Class.
This points to there being something fundamentally wrong with your Ruby installation. Class is a core Ruby class, and the Class constant should always be there. It's hard to diagnose with just the information you have given, but it appears that constant lookup is broken in your Ruby installation.
I recommend re-installing Ruby.

Related

How to hide Ruby file location when encountering error?

I have been working in many Rails applications and during the period I have found many bugs but one thing really pissed me off is the error showing the Ruby file name in the terminal.
I tried to run
User.find(3)
in the Rails console, and, though I don't have more than one user record, it was obviously showing an error and I was aware of it. But the location part really messed up my terminal.
Here is the error log:
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=3
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.0.beta2/lib/active_record/core.rb:150:in `find'
from (irb):79
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:110:in `start'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:9:in `start'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands.rb:17:in `<top (required)>'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `block in require'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /home/travis/tumpy/bin/rails:8:in `<top (required)>'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `block in load'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /home/travis/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/travis/.rvm/rubies/ruby-
The solution I was expecting is to hide the location part and only display the error, showing
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=3
IRB has a back_trace_limit option which defaults to 16:
irb(main):001:0> IRB.CurrentContext.back_trace_limit
#=> 16
Setting it to 0 suppresses the entire backtrace:
irb(main):002:0> IRB.CurrentContext.back_trace_limit = 0
#=> 0
irb(main):003:0> User.find(0)
# User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 0 LIMIT 1
#ActiveRecord::RecordNotFound: Couldn't find User with id=0
irb(main):004:0>
You can also pass this as a command line option:
$ rails console -- --back-trace-limit=0

rails console --sandbox michael hartl's rails tutorial 3rd edition not sure how this works

I have a question with Ruby on Rails using:
rails console --sandbox
So in Michael Hartl's rails tutorial 3rd edition we are using this and I have a problem when sometimes I'm using it and it all goes well and then I have an error, and literally the whole test environment becomes useless in that it will not respond to my commands
For example, I will type:
2.1.1 :025 > user.first
or any other command that was working and should be working and instead get:
NoMethodError: undefined method `first' for #<User:0x007fede35e3188>
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activemodel-4.2.0.beta2/lib/active_model/attribute_methods.rb:435:in `method_missing' from (irb):25
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2 gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:110:in `start'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2 gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:9:in `start'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/railties-4.2.0.beta2/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `block in require'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /Users/andrewkim/workspace2/sample_app/bin/rails:8:in `<top (required)>'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `block in load'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/commands/rails.rb:6:in `call'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/command_wrapper.rb:38:in `call'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:180:in `block in serve'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:153:in `fork'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:153:in `serve'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/andrewkim/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/andrewkim/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'2.1.1 :026 >
All of a sudden anything I type in terminal will come with an error like this, and I don't know how to go back without exiting "rails console --sandbox". is there a way?
for example, I will type:
2.1.1 :025 > user.first
or any other command that was working and should be working
That command shouldn't work, and it doesn't work. You are getting an error because in the tutorial a User instance, which represents one row in your table, e.g.
user = User.new(name: "Joe", email: "joe#yahoo.com")
does not have a method named first(). Instead, a user instance has the methods:
id()
name()
email()
created_at()
updated_at()
However, the class name(or model name) can be used to search the table, e.g.
user1 = User.first. #User is the name of the class/model
As for this:
and I don't know how to go back
Go back to what? Nothing has changed. All the variables you typed into the console before that error will still exist. Here is an example from the rails console:
$ rails console --sandbox
Loading development environment in sandbox (Rails 4.0.8)
Any modifications you make will be rolled back on exit
2.0.0-p481 :001 > x = 10
=> 10
2.0.0-p481 :002 > user = User.find_by(email: "example#railstutorial.org")
User Load (4.0ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'example#railstutorial.org' LIMIT 1
=> #<User id: 2, name: "Example User", email: "example#railstutorial.org", created_at: "2014-10-02 01:39:04", updated_at: "2014-10-02 01:39:04", password_digest: "$2a$10$VNzivk5opu1AC5DOM467dO2JaTg3c3JC9OAXR0AqvEi0...">
2.0.0-p481 :003 > user.first
NoMethodError: undefined method `first' for #<User:0x00000100f360c8>
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/activemodel-4.0.8/lib/active_model/attribute_methods.rb:439:in `method_missing'
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/activerecord-4.0.8/lib/active_record/attribute_methods.rb:168:in `method_missing'
from (irb):3
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/railties-4.0.8/lib/rails/commands/console.rb:90:in `start'
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/railties-4.0.8/lib/rails/commands/console.rb:9:in `start'
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/railties-4.0.8/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.0.0-p481 :004 > user
=> #<User id: 2, name: "Example User", email: "example#railstutorial.org", created_at: "2014-10-02 01:39:04", updated_at: "2014-10-02 01:39:04", password_digest: "$2a$10$VNzivk5opu1AC5DOM467dO2JaTg3c3JC9OAXR0AqvEi0...">
2.0.0-p481 :005 > x
=> 10
2.0.0-p481 :006 >
See? Everything is still there. On the other hand, if by "go back" you mean, "go back to the blissful state you were in when you got no errors", then the solution is to type the correct commands into the console. Next time, copy the command from the tutorial that you are trying to emulate and paste it into a blank text file, then copy the command that is giving you an error(but which you know is correct!) and paste it under the command from the tutorial:
User.first
user.first
Then compare them. Your problem can also be mitigated somewhat by never creating a variable that has the same name as your model, e.g. instead of writing:
user = User.new(...)
write:
my_user = User.new(...)
Then if you write:
user.first
You will get the error:
NameError: undefined local variable or method `user' for main:Object
which should be easier for you to debug.

Getting a lot of extra error information ".../commands/console.rb:..."

I'm working on a ruby on rails tutorial and using the rails console
When an error occurs I'm seeing a lot more error information than the tutorial shows (could be that they ommited it for brevity)
For example:
2.1.1 :001 > asdf
NameError: undefined local variable or method `asdf' for main:Object
from (irb):1
from /Users/me/.rvm/gems/ruby-2.1.1#global/gems/railties-4.0.4/lib/rails/commands/console.rb:90:in `start'
from /Users/me/.rvm/gems/ruby-2.1.1#global/gems/railties-4.0.4/lib/rails/commands/console.rb:9:in `start'
from /Users/me/.rvm/gems/ruby-2.1.1#global/gems/railties-4.0.4/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.1.1 :002 >
Whereas the tutorial would only show:
2.1.1 :001 > asdf
NameError: undefined local variable or method `asdf' for main:Object
Is that other information useful? Is there a way to suppress it?

Error instantiating mechanize object

I have no idea what the problem is on this. I'm trying this in the rails console:
>> agent = Mechanize.new
NoMethodError: undefined method `user' for nil:NilClass
I added gem 'mechanize' and did a bundle install. I did require 'mechanize'. I also tried require 'nokogiri' below that, and every time I get the same error. I've tried 4 or 5 different ways to instantiate a new mechanize object and haven't been able to find one.
I searched my entire rails app for the string '.user' and couldn't find any. Not sure what the deal is here. Any ideas?
Thanks
EDIT
Full Stack trace
NoMethodError: undefined method `user' for nil:NilClass
from /home/me/.rvm/gems/ruby-1.9.3-p448#my_app/gems/net-http-persistent-2.9.2/lib/net/http/persistent.rb:866:in `proxy='
from /home/me/.rvm/gems/ruby-1.9.3-p448#my_app/gems/mechanize-2.7.2/lib/mechanize/http/agent.rb:1189:in `set_proxy'
from /home/me/.rvm/gems/ruby-1.9.3-p448#my_app/gems/mechanize-2.7.2/lib/mechanize.rb:204:in `initialize'
from (irb):5:in `new'
from (irb):5
from /home/me/.rvm/gems/ruby-1.9.3-p448#global/gems/railties-3.2.15/lib/rails/commands/console.rb:47:in `start'
from /home/me/.rvm/gems/ruby-1.9.3-p448#global/gems/railties-3.2.15/lib/rails/commands/console.rb:8:in `start'
from /home/me/.rvm/gems/ruby-1.9.3-p448#global/gems/railties-3.2.15/lib/rails/commands.rb:41:in `<top (required)>'
from /home/me/Dropbox/Work/RubymineProjects/apps/my_app/script/rails:6:in `require'
from /home/me/Dropbox/Work/RubymineProjects/apps/my_app/script/rails:6:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
You're not doing anything wrong: the version of mechanize that came out a few days ago is buggy. I solved that error just updating the gem (i.e., with bundle update mechanize).

NameError: undefined local variable or method `user' for main:Object

I'm following through the Ruby on Rails tutorial, and when attempting to save a user in Rails Console (sandbox mode), I get the following error:
NameError: undefined local variable or method `user' for main:Object
from (irb):7
Note: I typed in User.new, input a name and email, then user.save, and got the error above.
Full code:
C:\Sites\rails_projects\sample_app>bundle exec rake db:migrate
== CreateUsers: migrating ====================================================
-- create_table(:users)
-> 0.0020s
== CreateUsers: migrated (0.0020s) ===========================================
C:\Sites\rails_projects\sample_app>rails console --sandbox
Loading development environment in sandbox (Rails 4.0.1)
Any modifications you make will be rolled back on exit
irb(main):001:0> User.new
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil>
<ame: "Andrew Ghobrial", email: "email#gmail.com")
=> #<User id: nil, name: "Andrew Ghobrial", email: "email#gmail.com",
created_at: nil, updated_at: nil>
irb(main):004:0> user.save
NameError: undefined local variable or method `user' for main:Object
from (irb):4
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
irb(main):006:0> user.save
NameError: undefined local variable or method `user' for main:Object
from (irb):6
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
irb(main):007:0> user
NameError: undefined local variable or method `user' for main:Object
from (irb):7
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'`
So, as #Phlip said, your User.new isn't assigned to a variable. If you do:
user = User.new(name: "Andrew Ghobrial", email: "email#gmail.com")
user.save
That will work, assuming your users table has name and email columns, and your user model looks something like:
class User << ActiveRecord::Base
attr_accessor :name, :email
end
While the capitalisation answers haven't been upvoted, the very first line of the original post IRB proves this was the issue. My resource was created with a lowercase title:
rails g resource user
After the db migration:
Typing "user.count" in IRB gets the NameError
Typing "User.count" gets the expected result (for my empty table):
irb(main):009:0> User.count
(0.4ms) SELECT COUNT(*) FROM "user"
=> 0
So the IRB is case sensitive on active resources and seemingly must be capitalised in declarations. Typing "User" will return a list of the field/column names.
1.9.3-p392 :004 > status.delete_all
in this Status 's' needs to be capitalized to 'S'.
or
in this case
NameError: undefined local variable or method `user' for main:Object
in this Status 'u' needs to be capitalized to 'U'.
or
in this case
NameError: undefined local variable or method `post' for main:Object
in this Status 'p' needs to be capitalized to 'P'.
Make sure that in (Resourse).user, The first letter (pin = Pin, or tweet = Tweet) is capitalized.

Resources