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

38,430

Solution 1

So, as @Phlip said, your User.new isn't assigned to a variable. If you do:

user = User.new(name: "Andrew Ghobrial", email: "[email protected]")

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

Solution 2

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.

Share:
38,430
Andrew
Author by

Andrew

Making life more pleasant and efficient, one line of code at a time.

Updated on October 02, 2020

Comments

  • Andrew
    Andrew over 3 years

    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 protected]")
    => #<User id: nil, name: "Andrew Ghobrial", email: "[email protected]",
    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>'`
    
  • Andrew
    Andrew over 10 years
    That still gives me this error: 'user' is not recognized as an internal or external command, operable program, or batch file. However, in my user.rb file under the models folder, I have: class User < Activerecord::Base end
  • CDub
    CDub over 10 years
    Do you have fields defined in the create_user migration for name and email? Those will need to be set on the User model. See my edit.
  • Andrew
    Andrew over 10 years
    Even after adding those fields, I'm still receiving that error. Even typing in user alone gives me that.
  • CDub
    CDub over 10 years
    Have you closed and restarted the rails console? In your app directory, do rails c
  • CDub
    CDub over 10 years
    It could also have to do with running in sandbox mode in development... You might want to try just straight development mode without sandbox (again, e.g. rails c)
  • Andrew
    Andrew over 10 years
    Now when I input: user = User.new(name: "Andrew Ghobrial", email: "[email protected]") It gives me the error syntax error, unexpected tLSHFT, expecting '<' or ';' or '\n' Pretty stumped myself.
  • CDub
    CDub over 10 years
    I'm still not convinced your app/models/user.rb is correct. Would you post that?
  • Andrew
    Andrew over 10 years
    class User << ActiveRecord::Base attr_accessor :name, :email end
  • CDub
    CDub over 10 years
    Dumb question, but if it's all on one line like that, it'll need to be: class User << ActiveRecord::Base; attr_accessor :name, :email; end - or did it just paste that way in the comment? It might be mor helpful to add that to your question so others can see it as well.
  • Nathan Tuggy
    Nathan Tuggy about 9 years
    This feels as though it's quite useful, but not for this question, which was verified as having a very different root cause than simple capitalization.