【问题标题】:Adding token_authenticatable to devise with migration添加 token_authenticable 以设计迁移
【发布时间】:2012-02-09 03:32:48
【问题描述】:

我已经使用 devise 创建了一个用户模型,但我现在想添加对 token_authenticable 的支持,所以我需要迁移这些添加。以下是否正确,token_authenticable 应该是什么类型?

class AddAuthenticationTokenToUser < ActiveRecord::Migration

  def change

    add_column :users, :token_authenticatable
    add_index  :users, :authentication_token, :unique => true

  end

end

【问题讨论】:

    标签: ruby-on-rails database devise migration token


    【解决方案1】:

    设计 2 中用于迁移的已删除助手的完整列表如下:

    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
    
      ## Encryptable
      # t.string :password_salt
    
      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable
    
      ## Lockable
      # t.integer  :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at
    
      # Token authenticatable
      # t.string :authentication_token
    
      ## Invitable
      # t.string :invitation_token
    
      t.timestamps
    end
    

    取自devise wiki

    【讨论】:

      【解决方案2】:

      来自 Github 上的 devise 2.0 generator(第 74 行):

      # t.string :authentication_token
      

      如果您要根据用户的令牌查找用户,那么添加索引是个好主意。

      Here is the devise 1.5 file

      【讨论】:

        【解决方案3】:
        add_column :users, :token_authenticatable, :string
        

        不要忘记将devise :token_authenticatable 添加到您的用户模型中。

        【讨论】:

        • 此架构样式在设计 2.0 中已更改。列名应为authentication_tokengithub.com/plataformatec/devise/wiki/…
        • rails g migration AddAuthenticationTokenToUser token_authenticatable:string:uniq
        猜你喜欢
        • 2012-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多