title: NoMethodError in Ruby on Rails calling Class Method from a controller
body: <p>I'm just learning RoR and have run into my first real bug. Would really appreciate some guidance with it. I am up to the point of creating a user sign in/out feature for a site I am building which will be maintained through sessions. When the sign in form submits it goes to a sessions controller which calls the 'create' method. When it does this it displays this error:</p>
  
  <blockquote>
    <p>NoMethodError in
    SessionsController#create</p>
    
    <p>You have a nil object when you didn't
    expect it! You might have expected an
    instance of ActiveRecord::Base. The
    error occurred while evaluating nil.[]</p>
  </blockquote>
  
  <p>Here's the code for the create method:</p>
  
  <pre><code>  33 def create
    34   user = User.authenticate(params[:session][:email],
    35                           params[:session][:password])
    36   if user.nil?
    37     flash.now[:error] = "Invalid email/password combination."
    38     @title = "Sign in"
    39     render 'new'
    40   else
    41     sign_in user
    42     redirect_back_or user
    43   end
    44 end
  </code></pre>
  
  <p>The application trace says the error is at </p>
  
  <p>app/controllers/sessions_controller.rb:34:in `create'</p>
  
  <p>So the issue seems to be with the authenticate method. This is a class method defined for my user object. What's weird is that I tested the method in rails console and it works fine. Below is the full trace. Again would be very grateful for some help with this. Thanks you.</p>
  
  <p>Trace:</p>
  
  <blockquote>
    <p>app/controllers/sessions_controller.rb:34:in
    <code>create' actionpack (3.0.4)
    lib/action_controller/metal/implicit_render.rb:4:in
    </code>send_action' actionpack (3.0.4)
    lib/abstract_controller/base.rb:150:in
    <code>process_action' actionpack (3.0.4)
    lib/action_controller/metal/rendering.rb:11:in
    </code>process_action' actionpack (3.0.4)
    lib/abstract_controller/callbacks.rb:18:in
    <code>block in process_action'
    activesupport (3.0.4)
    lib/active_support/callbacks.rb:435:in
    </code><em>run</em>_4224187041876590211__process_action_<em>3718750575726612430</em>_callbacks'
    activesupport (3.0.4)
    lib/active_support/callbacks.rb:409:in
    <code>_run_process_action_callbacks'
    activesupport (3.0.4)
    lib/active_support/callbacks.rb:93:in
    </code>run_callbacks' actionpack (3.0.4)
    lib/abstract_controller/callbacks.rb:17:in
    <code>process_action' actionpack (3.0.4)
    lib/action_controller/metal/instrumentation.rb:30:in
    </code>block in process_action'
    activesupport (3.0.4)
    lib/active_support/notifications.rb:52:in
    <code>block in instrument' activesupport
    (3.0.4)
    lib/active_support/notifications/instrumenter.rb:21:in
    </code>instrument' activesupport (3.0.4)
    lib/active_support/notifications.rb:52:in
    <code>instrument' actionpack (3.0.4)
    lib/action_controller/metal/instrumentation.rb:29:in
    </code>process_action' actionpack (3.0.4)
    lib/action_controller/metal/rescue.rb:17:in
    <code>process_action' actionpack (3.0.4)
    lib/abstract_controller/base.rb:119:in
    </code>process' actionpack (3.0.4)
    lib/abstract_controller/rendering.rb:41:in
    <code>process' actionpack (3.0.4)
    lib/action_controller/metal.rb:138:in
    </code>dispatch' actionpack (3.0.4)
    lib/action_controller/metal/rack_delegation.rb:14:in
    <code>dispatch' actionpack (3.0.4)
    lib/action_controller/metal.rb:178:in
    </code>block in action' actionpack (3.0.4)
    lib/action_dispatch/routing/route_set.rb:62:in
    <code>call' actionpack (3.0.4)
    lib/action_dispatch/routing/route_set.rb:62:in
    </code>dispatch' actionpack (3.0.4)
    lib/action_dispatch/routing/route_set.rb:27:in
    <code>call' rack-mount (0.6.14)
    lib/rack/mount/route_set.rb:148:in
    </code>block in call' rack-mount (0.6.14)
    lib/rack/mount/code_generation.rb:93:in
    <code>block in recognize' rack-mount
    (0.6.14)
    lib/rack/mount/code_generation.rb:68:in
    </code>optimized_each' rack-mount (0.6.14)
    lib/rack/mount/code_generation.rb:92:in
    <code>recognize' rack-mount (0.6.14)
    lib/rack/mount/route_set.rb:139:in
    </code>call' actionpack (3.0.4)
    lib/action_dispatch/routing/route_set.rb:492:in
    <code>call' actionpack (3.0.4)
    lib/action_dispatch/middleware/best_standards_support.rb:17:in
    </code>call' actionpack (3.0.4)
    lib/action_dispatch/middleware/head.rb:14:in
    <code>call' rack (1.2.2)
    lib/rack/methodoverride.rb:24:in
    </code>call' actionpack (3.0.4)
    lib/action_dispatch/middleware/params_parser.rb:21:in
    <code>call' actionpack (3.0.4)
    lib/action_dispatch/middleware/flash.rb:182:in
    </code>call' actionpack (3.0.4)
    lib/action_dispatch/middleware/session/abstract_store.rb:149:in
    <code>call' actionpack (3.0.4)
    lib/action_dispatch/middleware/cookies.rb:302:in</code>call' activerecord (3.0.4)
    lib/active_record/query_cache.rb:32:in
    <code>block in call' activerecord (3.0.4)
    lib/active_record/connection_adapters/abstract/query_cache.rb:28:in
    </code>cache' activerecord (3.0.4)
    lib/active_record/query_cache.rb:12:in
    <code>cache' activerecord (3.0.4)
    lib/active_record/query_cache.rb:31:in
    </code>call' activerecord (3.0.4)
    lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in
    <code>call' actionpack (3.0.4)
    lib/action_dispatch/middleware/callbacks.rb:46:in
    </code>block in call' activesupport (3.0.4)
    lib/active_support/callbacks.rb:415:in
    <code>_run_call_callbacks' actionpack
    (3.0.4)
    lib/action_dispatch/middleware/callbacks.rb:44:in
    </code>call' rack (1.2.2)
    lib/rack/sendfile.rb:107:in <code>call'
    actionpack (3.0.4)
    lib/action_dispatch/middleware/remote_ip.rb:48:in
    </code>call' actionpack (3.0.4)
    lib/action_dispatch/middleware/show_exceptions.rb:47:in
    <code>call' railties (3.0.4)
    lib/rails/rack/logger.rb:13:in</code>call'
    rack (1.2.2) lib/rack/runtime.rb:17:in
    <code>call' activesupport (3.0.4)
    lib/active_support/cache/strategy/local_cache.rb:72:in
    </code>call' rack (1.2.2)
    lib/rack/lock.rb:11:in <code>block in call'
    &lt;internal:prelude&gt;:10:in</code>synchronize'
    rack (1.2.2) lib/rack/lock.rb:11:in
    <code>call' actionpack (3.0.4)
    lib/action_dispatch/middleware/static.rb:30:in
    </code>call' railties (3.0.4)
    lib/rails/application.rb:168:in <code>call'
    railties (3.0.4)
    lib/rails/application.rb:77:in
    </code>method_missing' railties (3.0.4)
    lib/rails/rack/log_tailer.rb:14:in
    <code>call' rack (1.2.2)
    lib/rack/content_length.rb:13:in
    </code>call' rack (1.2.2)
    lib/rack/handler/webrick.rb:52:in
    <code>service'
    /Users/USERNAME_REMOVED/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/httpserver.rb:111:in
    </code>service'
    /Users/USERNAME_REMOVED/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/httpserver.rb:70:in
    <code>run'
    /Users/USERNAME_REMOVED/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/server.rb:183:in
    </code>block in start_thread'</p>
  </blockquote>