设计迁移不起作用

时间:2011-06-17 22:52:03

标签: ruby-on-rails devise rake

我使用Devise创建了一个用户模型,每当我使用rake db:migrate时,我都会遇到以下问题:

==  DeviseCreateUsers: migrating ==============================================
-- create_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:

undefined method `database_authenticatable' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x00000103fa1ff0>

Tasks: TOP => db:migrate
(See full trace by running task with --trace)

我使用--trace function运行完整跟踪,但似乎无法弄清问题是什么。 这是迁移文件:

class DeviseCreateUsers < ActiveRecord::Migration
def self.up
create_table(:users) do |t|
  t.database_authenticatable :null => false
  t.recoverable
  t.rememberable
  t.trackable
  t.confirmable

  # t.encryptable
  # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
  # t.token_authenticatable


  t.timestamps
 end

add_index :users, :email,                :unique => true
add_index :users, :reset_password_token, :unique => true
add_index :users, :confirmation_token,   :unique => true
# add_index :users, :unlock_token,         :unique => true
# add_index :users, :authentication_token, :unique => true
end

def self.down
  drop_table :users
  end
end

2 个答案:

答案 0 :(得分:22)

Devise 2.0消除了用于创建数据库表的辅助函数:

https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.0-migration-schema-style

所以你需要的是:

create_table(TABLE_NAME) do |t|
  ## Database authenticatable

  t.string :email,              :null => false, :default => ""
  t.string :encrypted_password, :null => false, :default => ""

  ## Recoverable
  t.string   :reset_password_token
  t.datetime :reset_password_sent_at

  ## Rememberable
  t.datetime :remember_created_at

  ## Trackable
  t.integer  :sign_in_count, :default => 0
  t.datetime :current_sign_in_at
  t.datetime :last_sign_in_at
  t.string   :current_sign_in_ip
  t.string   :last_sign_in_ip

  t.timestamps

end

答案 1 :(得分:2)

很高兴听到。 我想官方的答案是重新运行这些步骤:

rails g devise:install

rails g devise user

rake db:migrate

rails s //循环你的服务器以防万一...