undefined method `empty?` for nil:NilClass <p>When I was trying to run rake db:migrate on this open source project called expertiza (link:<a href="https://github.com/expertiza/expertiza" rel="nofollow">https://github.com/expertiza/expertiza</a>), I got this error message. The full message is showing below. I think the problem is probably something wrong with the environment set up. Is there any way to fix this bug without changing the code?</p> <p>Error message:</p> <pre><code>rake aborted! StandardError: An error has occurred, all later migrations canceled: undefined method `empty?' for nil:NilClass /home/maxinghua/expertiza/app/models/menu.rb:81:in `initialize' /home/maxinghua/expertiza/app/models/role.rb:87:in `new' /home/maxinghua/expertiza/app/models/role.rb:87:in `rebuild_menu' /home/maxinghua/expertiza/app/models/role.rb:73:in `block in rebuild_cache' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/relation/batches.rb:51:in `block (2 levels) in find_each' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/relation/batches.rb:51:in `each' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/relation/batches.rb:51:in `block in find_each' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/relation/batches.rb:124:in `find_in_batches' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/relation/batches.rb:50:in `find_each' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/querying.rb:9:in `find_each' /home/maxinghua/expertiza/app/models/role.rb:67:in `rebuild_cache' /home/maxinghua/expertiza/db/migrate/002_initialize_custom.rb:184:in `up' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:571:in `up' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:611:in `exec_migration' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:592:in `block (2 levels) in migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:591:in `block in migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:590:in `migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:768:in `migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:1046:in `ddl_transaction' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:997:in `execute_migration_in_transaction' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:959:in `block in migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:955:in `each' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:955:in `migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:823:in `up' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:801:in `migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:137:in `migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/railties/databases.rake:44:in `block (2 levels) in &lt;top (required)&gt;' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/airbrake-5.5.0/lib/airbrake/rake/task_ext.rb:19:in `execute' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/rake-11.2.2/exe/rake:27:in `&lt;top (required)&gt;' /home/maxinghua/.rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in `eval' /home/maxinghua/.rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in `&lt;main&gt;' NoMethodError: undefined method `empty?' for nil:NilClass /home/maxinghua/expertiza/app/models/menu.rb:81:in `initialize' /home/maxinghua/expertiza/app/models/role.rb:87:in `new' /home/maxinghua/expertiza/app/models/role.rb:87:in `rebuild_menu' /home/maxinghua/expertiza/app/models/role.rb:73:in `block in rebuild_cache' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/relation/batches.rb:51:in `block (2 levels) in find_each' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/relation/batches.rb:51:in `each' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/relation/batches.rb:51:in `block in find_each' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/relation/batches.rb:124:in `find_in_batches' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/relation/batches.rb:50:in `find_each' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/querying.rb:9:in `find_each' /home/maxinghua/expertiza/app/models/role.rb:67:in `rebuild_cache' /home/maxinghua/expertiza/db/migrate/002_initialize_custom.rb:184:in `up' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:571:in `up' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:611:in `exec_migration' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:592:in `block (2 levels) in migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:591:in `block in migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:590:in `migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:768:in `migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:1046:in `ddl_transaction' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:997:in `execute_migration_in_transaction' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:959:in `block in migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:955:in `each' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:955:in `migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:823:in `up' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/migration.rb:801:in `migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:137:in `migrate' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.6/lib/active_record/railties/databases.rake:44:in `block (2 levels) in &lt;top (required)&gt;' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/airbrake-5.5.0/lib/airbrake/rake/task_ext.rb:19:in `execute' /home/maxinghua/.rvm/gems/ruby-2.2.4/gems/rake-11.2.2/exe/rake:27:in `&lt;top (required)&gt;' /home/maxinghua/.rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in `eval' /home/maxinghua/.rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in `&lt;main&gt;' Tasks: TOP =&gt; db:migrate (See full trace by running task with --trace) </code></pre> <p>This is the reported <code>empty?</code> method Code in menu.rb:81:in <code>initialize</code>:</p> <pre><code>if role unless role.cache[:credentials].permission_ids.empty? items = MenuItem.items_for_permissions(role.cache[:credentials].permission_ids) end else # No role given: build menu of everything items = MenuItem.items_for_permissions end </code></pre>