未定义的方法`to_sym' for nil:迁移后的NilClass

时间:2014-10-11 03:07:23

标签: ruby-on-rails ruby devise cancan rolify

我在Rails项目中使用devise,cancan和rolify遵循此Rolify Tutorial。创建新的迁移(运行正常)后,我收到此错误:

  

未定义的方法`to_sym'为零:NilClass

我不知道自己做得不好。这是我的调试。

我改变用户表的迁移是:

class AlterUsersTable1 < ActiveRecord::Migration
  def up
    change_table :users, :id => true do |t|
      t.references :genre, :null => false, :default => 1
      t.string :first_name, :null => false, :default => ''
      t.string :last_name, :null => false, :default => ''
      t.string :company, :null => true
      t.boolean :status, :null => false, :default => true
  end

  execute <<-SQL
    ALTER TABLE users 
    ADD CONSTRAINT fk_users_genre
    FOREIGN KEY (genre_id) 
    REFERENCES genres(id)
  SQL

  execute <<-SQL
    ALTER TABLE users 
    DROP PRIMARY KEY, ADD PRIMARY KEY (id, genre_id)    
  SQL
end

# RESET THE TABLE
def down
  execute <<-SQL
    ALTER TABLE users 
    DROP FOREIGN KEY fk_users_genre
  SQL

  execute <<-SQL
    ALTER TABLE users
    DROP PRIMARY KEY, ADD PRIMARY KEY (id)    
  SQL

  remove_column :users, :first_name
  remove_column :users, :last_name
  remove_column :users, :company
  remove_column :users, :status
  remove_column :users, :genre_id
end
end

我的路线档案是:

  devise_for :users
  resources :users
  resources :categories
  root :to => 'home#index'

application_controller.rb

  before_filter :authenticate_user!

这是我的追踪

activerecord (3.2.19) lib/active_record/sanitization.rb:60:in `block in expand_hash_conditions_for_aggregates'
activerecord (3.2.19) lib/active_record/sanitization.rb:59:in `each'
activerecord (3.2.19) lib/active_record/sanitization.rb:59:in `expand_hash_conditions_for_aggregates'
activerecord (3.2.19) lib/active_record/relation/query_methods.rb:326:in `build_where'
activerecord (3.2.19) lib/active_record/relation/query_methods.rb:136:in `where'
activerecord (3.2.19) lib/active_record/querying.rb:9:in `where'
orm_adapter (0.5.0) lib/orm_adapter/adapters/active_record.rb:17:in `get'
devise (3.2.2) lib/devise/models/authenticatable.rb:208:in `serialize_from_session'
devise (3.2.2) lib/devise.rb:462:in `block (2 levels) in configure_warden!'
warden (1.2.3) lib/warden/session_serializer.rb:34:in `fetch'
warden (1.2.3) lib/warden/proxy.rb:212:in `user'
warden (1.2.3) lib/warden/proxy.rb:318:in `_perform_authentication'
warden (1.2.3) lib/warden/proxy.rb:104:in `authenticate'
warden (1.2.3) lib/warden/proxy.rb:114:in `authenticate?'
devise (3.2.2) app/controllers/devise_controller.rb:103:in `require_no_authentication'
activesupport (3.2.19) lib/active_support/callbacks.rb:407:in `_run__584588561542237872__process_action__1933424417154408597__callbacks'
activesupport (3.2.19) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.19) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.19) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.19) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.19) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.19) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.19) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.19) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.19) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.19) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.19) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.19) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.19) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.19) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.19) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.19) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.19) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.19) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.19) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.19) lib/action_dispatch/routing/route_set.rb:36:in `call'
actionpack (3.2.19) lib/action_dispatch/routing/mapper.rb:43:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block 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.19) lib/action_dispatch/routing/route_set.rb:608:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.19) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.19) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.19) lib/active_support/callbacks.rb:405:in `_run__3633975209017476260__call__1066395091297338092__callbacks'
activesupport (3.2.19) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.19) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.19) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.19) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.19) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.19) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.19) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.19) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.19) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.19) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.19) lib/rails/engine.rb:484:in `call'
railties (3.2.19) lib/rails/application.rb:231:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.19) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/Users/oscar/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/oscar/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/oscar/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

1 个答案:

答案 0 :(得分:1)

好的,我不知道为什么,但删除自定义SQL并调用此方法现在一切正常,但没有外键:(

add_column :users, :genre_id, :integer, :default => 1, :null => false
add_index :users, :genre_id