如果用户模型已经存在,是否可以添加设计? [英] Is it possible to add devise, if user model already exists?

查看:104
本文介绍了如果用户模型已经存在,是否可以添加设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经在项目开始时创建了一个用户模型,但现在(稍后会有几个迁移),我们想使用devise gem。如果用户模型和表已经存在,是否可以添加设计?也就是说,是否可以改变已经完成的任务,还是要重新开始?

We already created a user model in the beggining of the project, but now (several migrations later) we would like to use the devise gem. Is it possible to add devise if the user model and table already exist? That is, is it possible to alter what is already done, or do we have to start all over again?

推荐答案

Cavert Coder ,但是:

Cavert Coder, but:

(注意,这不会迁移:lockable,因为我在写它时不在乎它现在包括:可锁定,因为MattSlay比我以前关心:)此外,您需要将用户密码迁移到加密密码字段。最后,它可能不适合你。对不起。)

(Note, this doesn't migrate ":lockable" because I didn't care about it when I wrote it This now includes :lockable because MattSlay cared more than I did :). Also, you'll need to migrate your users passwords into the encrypted passwords field. Finally, it might not work for you. Sorry.)

class AddDevise < ActiveRecord::Migration
  def self.up
    null    = false
    default = ""

    add_column :users, :encrypted_password, :string, :null => null, :default => default, :limit => 128
    add_column :users, :password_salt, :string
    add_column :users, :authentication_token, :string
    add_column :users, :confirmation_token,   :string
    add_column :users, :confirmed_at,         :datetime
    add_column :users, :confirmation_sent_at, :datetime
    add_column :users, :reset_password_token, :string
    add_column :users, :remember_token,      :string
    add_column :users, :remember_created_at, :datetime
    add_column :users, :sign_in_count,      :integer, :default => 0
    add_column :users, :current_sign_in_at, :datetime
    add_column :users, :last_sign_in_at,    :datetime
    add_column :users, :current_sign_in_ip, :string
    add_column :users, :last_sign_in_ip,    :string

    #:lockable fields contributed by MattSlay
    add_column :users, :failed_attempts, :integer, :default => 0
    add_column :users, :unlock_token,   :string
    add_column :users, :locked_at, :datetime

  end

  def self.down
    remove_column :users, :encrypted_password
    remove_column :users, :password_salt
    remove_column :users, :authentication_token
    remove_column :users, :confirmation_token
    remove_column :users, :confirmed_at
    remove_column :users, :confirmation_sent_at
    remove_column :users, :reset_password_token
    remove_column :users, :remember_token
    remove_column :users, :remember_created_at
    remove_column :users, :sign_in_count
    remove_column :users, :current_sign_in_at
    remove_column :users, :last_sign_in_at
    remove_column :users, :current_sign_in_ip
    remove_column :users, :last_sign_in_ip
    remove_column :users, :failed_attempts
    remove_column :users, :unlock_token
    remove_column :users, :locked_at
  end
end

这篇关于如果用户模型已经存在,是否可以添加设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆