Thursday, April 28, 2011

Getting "undefined method `name' for nil:NilClass" when running command "rake devise:setup"

I have a Rails application with
Rails version-3.0.5,
Ruby version- "ruby 1.8.7 (2010-08-16 patchlevel 302) [i386-mingw32]" and devise gem which is working fine. But when I am running command "rake devise:setup" it gives me following error:
rake aborted!
undefined method `name' for nil:NilClass

(See full trace by running task with --trace)

On running command with full trace following is the error:

** Invoke db:schema:dump (first_time)
** Invoke environment
** Execute db:schema:dump
** Invoke environment
** Execute devise:setup
rake aborted!
undefined method `name' for nil:NilClass
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/whiny_nil.rb:48:in `method_missing'
C:/Ruby187/lib/ruby/gems/1.8/gems/arel-2.0.9/lib/arel/visitors/to_sql.rb:57:in `visit_Arel_Nodes_InsertStatement'
C:/Ruby187/lib/ruby/gems/1.8/gems/arel-2.0.9/lib/arel/visitors/to_sql.rb:56:in `map'
C:/Ruby187/lib/ruby/gems/1.8/gems/arel-2.0.9/lib/arel/visitors/to_sql.rb:56:in `visit_Arel_Nodes_InsertStatement'
C:/Ruby187/lib/ruby/gems/1.8/gems/arel-2.0.9/lib/arel/visitors/visitor.rb:15:in `send'
C:/Ruby187/lib/ruby/gems/1.8/gems/arel-2.0.9/lib/arel/visitors/visitor.rb:15:in `visit'
C:/Ruby187/lib/ruby/gems/1.8/gems/arel-2.0.9/lib/arel/visitors/visitor.rb:5:in `accept'
C:/Ruby187/lib/ruby/gems/1.8/gems/arel-2.0.9/lib/arel/visitors/to_sql.rb:19:in `accept'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:111:in `with_connection'
C:/Ruby187/lib/ruby/gems/1.8/gems/arel-2.0.9/lib/arel/visitors/to_sql.rb:17:in `accept'
C:/Ruby187/lib/ruby/gems/1.8/gems/arel-2.0.9/lib/arel/tree_manager.rb:20:in `to_sql'
C:/Ruby187/lib/ruby/gems/1.8/gems/arel-2.0.9/lib/arel/select_manager.rb:217:in `insert'
C:0:in `__send__'
C:0:in `insert'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/persistence.rb:270:in `create'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/timestamp.rb:47:in `create'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/callbacks.rb:281:in `create'
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/callbacks.rb:420:in `_run_create_callbacks'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/callbacks.rb:281:in `create'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/persistence.rb:246:in `create_or_update'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/callbacks.rb:277:in `create_or_update'
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.5/lib/active_support/callbacks.rb:418:in `_run_save_callbacks'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/callbacks.rb:277:in `create_or_update'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/persistence.rb:39:in `save'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/validations.rb:43:in `save'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/attribute_methods/dirty.rb:21:in `save'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/transactions.rb:240:in `save'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/transactions.rb:292:in `with_transaction_returning_status'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/transactions.rb:207:in `transaction'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/transactions.rb:240:in `save'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/transactions.rb:251:in `rollback_active_record_state!'
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/transactions.rb:239:in `save'
C:/Ruby187/lib/ruby/gems/1.8/gems/devise-1.3.3/lib/devise/models/token_authenticatable.rb:41:in `reset_authentication_token!'
C:/Ruby187/lib/ruby/gems/1.8/gems/devise-1.3.3/lib/devise/models/token_authenticatable.rb:51:in `ensure_authentication_token!'
C:/projects/restaurant/lib/tasks/setup.rake:9
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/validations.rb:33:in `create!'
C:/projects/restaurant/lib/tasks/setup.rake:5
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
C:/Ruby187/lib/ruby/1.8/monitor.rb:242:in `synchronize'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31
C:/Ruby187/bin/rake:19:in `load'
C:/Ruby187/bin/rake:19

Thanks in advance for any help/solution.

2 comments:

  1. After debugging I found that rake task "rake devise:setup" when executed does following action - drop db, create db, migrate the db and then create normal user and admin user.
    Code of setup.rake is as follows:

    namespace :devise do

    desc 'setup devise example migrating db and creating a default user'
    task :setup => ['db:drop', 'db:create', 'db:migrate', 'environment'] do
    user = User.create! do |u|
    u.email = 'ritesh@test.com'
    u.password = 'riteshabc'
    u.password_confirmation = 'riteshabc'
    u.ensure_authentication_token!
    end
    user.confirm!
    puts 'New user created!'
    puts 'Email : ' << user.email
    puts 'Password: ' << user.password

    admin = Admin.create! do |u|
    u.email = 'admin@ritesh.com'
    u.password = 'riteshabc'
    u.password_confirmation = 'riteshabc'
    end
    #admin.confirm!
    puts 'New admin created!'
    puts 'Email : ' << admin.email
    puts 'Password: ' << admin.password
    end
    end

    Somehow, code of creation of user and admin when executed with devise setup is creating problem. I also tried to put the user and admin creation code in a new migration and then executed the devise setup, but got the same problem.
    So, I came across a solution, I put the code in a new rake task instead of putting in migration. And after execution of devise setup rake task, I need to execute the new rake task manually. Although I don’t prefer this alternate solution but definitely a work around solution.

    Here is the final code:

    namespace :devise do

    desc 'setup devise example migrating db and creating a default user'
    task :setup => ['db:drop', 'db:create', 'db:migrate', 'environment'] do

    end

    task :create_users => :environment do
    user = User.create! do |u|
    u.email = 'ritesh@test.com'
    u.password = 'riteshabc'
    u.password_confirmation = 'riteshabc'
    u.ensure_authentication_token!
    end
    user.confirm!
    puts 'New user created!'
    puts 'Email : ' << user.email
    puts 'Password: ' << user.password

    admin = Admin.create! do |u|
    u.email = 'admin@ritesh.com'
    u.password = 'riteshabc'
    u.password_confirmation = 'riteshabc'
    end
    #admin.confirm!
    puts 'New admin created!'
    puts 'Email : ' << admin.email
    puts 'Password: ' << admin.password
    end
    end

    After executing rake task “rake devise:setup”, need to execute another rake task “rake devise: create_users” to create initial user and admin.

    ReplyDelete
  2. Similar issue - https://github.com/rails/arel/issues/124

    ReplyDelete