undefined method `to_sym' for nil:NilClass

Hi all,

I am trying to upgrade my rails 2 app to 3 and am getting this error when trying to login:

NoMethodError in AuthenticateController#authenticate

undefined method `to_sym' for nil:NilClass

The stack trace is:

activerecord (3.2.2) lib/active_record/validations/uniqueness.rb:26:in `validate_each' activemodel (3.2.2) lib/active_model/validator.rb:153:in `validate' activemodel (3.2.2) lib/active_model/validator.rb:150:in `each' activemodel (3.2.2) lib/active_model/validator.rb:150:in `validate' activesupport (3.2.2) lib/active_support/callbacks.rb:310:in `send' activesupport (3.2.2) lib/active_support/callbacks.rb:310:in `_callback_before_15' activesupport (3.2.2) lib/active_support/callbacks.rb:418:in `_run__1200818654__validate__4__callbacks' activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `send' activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `__run_callback' activesupport (3.2.2) lib/active_support/callbacks.rb:385:in `_run_validate_callbacks' activesupport (3.2.2) lib/active_support/callbacks.rb:81:in `send' activesupport (3.2.2) lib/active_support/callbacks.rb:81:in `run_callbacks' activemodel (3.2.2) lib/active_model/validations.rb:212:in `run_validations!' activemodel (3.2.2) lib/active_model/validations/callbacks.rb:53:in `run_validations!' activesupport (3.2.2) lib/active_support/callbacks.rb:403:in `_run__1200818654__validation__4__callbacks' activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `send' activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `__run_callback' activesupport (3.2.2) lib/active_support/callbacks.rb:385:in `_run_validation_callbacks' activesupport (3.2.2) lib/active_support/callbacks.rb:81:in `send' activesupport (3.2.2) lib/active_support/callbacks.rb:81:in `run_callbacks' activemodel (3.2.2) lib/active_model/validations/callbacks.rb:53:in `run_validations!' activemodel (3.2.2) lib/active_model/validations.rb:179:in `valid?' activerecord (3.2.2) lib/active_record/validations.rb:69:in `valid?' activerecord (3.2.2) lib/active_record/validations.rb:77:in `perform_validations' activerecord (3.2.2) lib/active_record/validations.rb:50:in `save' activerecord (3.2.2) lib/active_record/attribute_methods/dirty.rb:22:in `save' activerecord (3.2.2) lib/active_record/transactions.rb:241:in `save' activerecord (3.2.2) lib/active_record/transactions.rb:295:in `with_transaction_returning_status' activerecord (3.2.2) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction' activerecord (3.2.2) lib/active_record/transactions.rb:208:in `transaction' activerecord (3.2.2) lib/active_record/transactions.rb:293:in `with_transaction_returning_status' activerecord (3.2.2) lib/active_record/transactions.rb:241:in `save' activerecord (3.2.2) lib/active_record/transactions.rb:252:in `rollback_active_record_state!' activerecord (3.2.2) lib/active_record/transactions.rb:240:in `save' app/models/user.rb:69:in `authenticate' app/controllers/authenticate_controller.rb:9:in `authenticate' actionpack (3.2.2) lib/action_controller/metal/implicit_render.rb:4:in `send_action' actionpack (3.2.2) lib/action_controller/metal/implicit_render.rb:4:in `send_action' actionpack (3.2.2) lib/abstract_controller/base.rb:167:in `process_action' actionpack (3.2.2) lib/action_controller/metal/rendering.rb:10:in `process_action' actionpack (3.2.2) lib/abstract_controller/callbacks.rb:18:in `process_action' activesupport (3.2.2) lib/active_support/callbacks.rb:414:in `_run__2116553026__process_action__1148928051__callbacks' activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `send' activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `__run_callback' activesupport (3.2.2) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks' activesupport (3.2.2) lib/active_support/callbacks.rb:81:in `send' activesupport (3.2.2) lib/active_support/callbacks.rb:81:in `run_callbacks' actionpack (3.2.2) lib/abstract_controller/callbacks.rb:17:in `process_action' actionpack (3.2.2) lib/action_controller/metal/rescue.rb:29:in `process_action' actionpack (3.2.2) lib/action_controller/metal/instrumentation.rb:30:in `process_action' activesupport (3.2.2) lib/active_support/notifications.rb:123:in `instrument' activesupport (3.2.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument' activesupport (3.2.2) lib/active_support/notifications.rb:123:in `instrument' actionpack (3.2.2) lib/action_controller/metal/instrumentation.rb:29:in `process_action' actionpack (3.2.2) lib/action_controller/metal/params_wrapper.rb:205:in `process_action' activerecord (3.2.2) lib/active_record/railties/controller_runtime.rb:18:in `process_action' actionpack (3.2.2) lib/abstract_controller/base.rb:121:in `process' actionpack (3.2.2) lib/abstract_controller/rendering.rb:45:in `process' actionpack (3.2.2) lib/action_controller/metal.rb:203:in `dispatch' actionpack (3.2.2) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' actionpack (3.2.2) lib/action_controller/metal.rb:246:in `action' actionpack (3.2.2) lib/action_dispatch/routing/route_set.rb:67:in `call' actionpack (3.2.2) lib/action_dispatch/routing/route_set.rb:67:in `dispatch' actionpack (3.2.2) lib/action_dispatch/routing/route_set.rb:30:in `call' journey (1.0.4) lib/journey/router.rb:68:in `call' journey (1.0.4) lib/journey/router.rb:56:in `each' journey (1.0.4) lib/journey/router.rb:56:in `call' actionpack (3.2.2) lib/action_dispatch/routing/route_set.rb:594:in `call' actionpack (3.2.2) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' rack (1.4.1) lib/rack/etag.rb:23:in `call' rack (1.4.1) lib/rack/conditionalget.rb:35:in `call' actionpack (3.2.2) lib/action_dispatch/middleware/head.rb:14:in `call' actionpack (3.2.2) lib/action_dispatch/middleware/params_parser.rb:21:in `call' actionpack (3.2.2) lib/action_dispatch/middleware/flash.rb:242:in `call' rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context' rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call' actionpack (3.2.2) lib/action_dispatch/middleware/cookies.rb:338:in `call' activerecord (3.2.2) lib/active_record/query_cache.rb:64:in `call' activerecord (3.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `call' actionpack (3.2.2) lib/action_dispatch/middleware/callbacks.rb:28:in `call' activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `_run__856567150__call__4__callbacks' activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `send' activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `__run_callback' activesupport (3.2.2) lib/active_support/callbacks.rb:385:in `_run_call_callbacks' activesupport (3.2.2) lib/active_support/callbacks.rb:81:in `send' activesupport (3.2.2) lib/active_support/callbacks.rb:81:in `run_callbacks' actionpack (3.2.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call' actionpack (3.2.2) lib/action_dispatch/middleware/reloader.rb:65:in `call' actionpack (3.2.2) lib/action_dispatch/middleware/remote_ip.rb:31:in `call' actionpack (3.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call' actionpack (3.2.2) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' railties (3.2.2) lib/rails/rack/logger.rb:26:in `call_app' railties (3.2.2) lib/rails/rack/logger.rb:16:in `call' actionpack (3.2.2) lib/action_dispatch/middleware/request_id.rb:22:in `call' rack (1.4.1) lib/rack/methodoverride.rb:21:in `call' rack (1.4.1) lib/rack/runtime.rb:17:in `call' activesupport (3.2.2) lib/active_support/cache/strategy/local_cache.rb:72:in `call' rack (1.4.1) lib/rack/lock.rb:15:in `call' actionpack (3.2.2) lib/action_dispatch/middleware/static.rb:61:in `call' railties (3.2.2) lib/rails/engine.rb:479:in `call' railties (3.2.2) lib/rails/application.rb:220:in `call' rack (1.4.1) lib/rack/content_length.rb:14:in `call' railties (3.2.2) lib/rails/rack/log_tailer.rb:14:in `call' rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service' /home/shandy/.rvm/rubies/ruby-1.8.7-p370/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' /home/shandy/.rvm/rubies/ruby-1.8.7-p370/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' /home/shandy/.rvm/rubies/ruby-1.8.7-p370/lib/ruby/1.8/webrick/server.rb:173:in `start_thread' /home/shandy/.rvm/rubies/ruby-1.8.7-p370/lib/ruby/1.8/webrick/server.rb:162:in `start' /home/shandy/.rvm/rubies/ruby-1.8.7-p370/lib/ruby/1.8/webrick/server.rb:162:in `start_thread' /home/shandy/.rvm/rubies/ruby-1.8.7-p370/lib/ruby/1.8/webrick/server.rb:95:in `start' /home/shandy/.rvm/rubies/ruby-1.8.7-p370/lib/ruby/1.8/webrick/server.rb:92:in `each' /home/shandy/.rvm/rubies/ruby-1.8.7-p370/lib/ruby/1.8/webrick/server.rb:92:in `start' /home/shandy/.rvm/rubies/ruby-1.8.7-p370/lib/ruby/1.8/webrick/server.rb:23:in `start' /home/shandy/.rvm/rubies/ruby-1.8.7-p370/lib/ruby/1.8/webrick/server.rb:82:in `start' rack (1.4.1) lib/rack/handler/webrick.rb:13:in `run' rack (1.4.1) lib/rack/server.rb:265:in `start' railties (3.2.2) lib/rails/commands/server.rb:70:in `start' railties (3.2.2) lib/rails/commands.rb:55 railties (3.2.2) lib/rails/commands.rb:50:in `tap' railties (3.2.2) lib/rails/commands.rb:50 script/rails:6:in `require' script/rails:6

About halfway down I notice an error directing me to my User model in method 'authenticate' and on line 69. When I go to that line I see this:

if !@user.blank?   if !(@user.password_hash.blank?) and !(@user.password_salt.blank?)     if @user.blank? ||       Digest::SHA256.hexdigest(password+@user.password_salt) !=       @user.password_hash         #raise "Unknown userid or password"     else   @user.last_login = Time.now   @user.save # <---- This is Line 69   @user     end   end end

The error seems to be trying to tell me that I have a nil object, but because I have this in an if block that checks for such a condition I find this a little hard to believe.

Just wondering if anybody has any ideas as I have done many-a Google search and have come up empty? Thanks

-S

NoMethodError in AuthenticateController#authenticate

undefined method `to_sym' for nil:NilClass

...

About halfway down I notice an error directing me to my User model in method 'authenticate' and on line 69. When I go to that line I see this:

...

  @user.save # <---- This is Line 69

...

The error seems to be trying to tell me that I have a nil object, but because I have this in an if block that checks for such a condition I find this a little hard to believe.

Wow, that one's MUCH more complex than it looked at first! 8-O

As you've figured out, most of the time that error message is seen, someone's simply got a nil object, usually from forgetting to check if a query succeeded or some such. But you've taken care of that properly.

I found uniqueness.rb at https://github.com/lifo/docrails/blob/master/activerecord/lib/active_record/validations/uniqueness.rb and line 26 says:

  relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?

So, finder_class is the offending object, and has a nil primary key. (finder_class itself isn't nil or it would have barfed earlier.) So what the fillintheblank is finder_class?! If I grok the code in fullness (it's set by a call to the method defined at line 56), it's the first non-abstract class in the chain from the class that the validator is on, to the class of the object being validated, including both. Assuming that your User class is not abstract, that would probably be it. (Seems to me they could save some time by just checking the top class first, but that's another story.)

So, it seems to me that it's saying that your users table does not have a primary key declared. Does it?

Not having one would be rather odd. Most of the usual ways to create a model, declare one....

-Dave

That was it exactly! Sorry it took me so long to reply, I had some fires that needed to be stomped out. Thanks again,

-S