1. rails generate migration add_index_to_users_email
将email 设置为user表的索引键
在 db/migrate文件夹下就生成了xxxxxxxxx_add_index_to_users_email.rb文件
打开该文件显示
class AddIndexToUsersEmail < ActiveRecord::Migration
def change
end
end
既然是索引键就必须保证它的唯一性
给change方法加一句
add_index :users, :email, unique: true文件内容修改为
class AddIndexToUsersEmail < ActiveRecord::Migration
def change
add_index :users, :email, unique: true
end
end
然后执行数据库的升迁命令
bundle exec rake db:migrate
2,添加安全密码
把 bcrypt-ruby 加入 Gemfile
执行bundle install导入应用包
生成一个合适的迁移文件,添加 password_digest
列:
rails generate migration add_password_digest_to_users password_digest:string
db/migrate下生成[ts]_add_password_digest_to_users.rb
打开后为change方法添加
add_column :users, :password_digest, :string
调用add_column方法将password_digest添加到user表中
执行数据升迁命令