Andrew K
Andrew K

Reputation: 1

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?

Upvotes: 0

Views: 915

Answers (1)

7stud
7stud

Reputation: 48609

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: "[email protected]")

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: "[email protected]")
  User Load (4.0ms)  SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1
 => #<User id: 2, name: "Example User", email: "[email protected]", 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: "[email protected]", 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.

Upvotes: 3

Related Questions