简体   繁体   English

Rake Migration:如何为所有用户输入auth_token到迁移中

[英]Rake Migration: How to enter an auth_token into a migration for all users

I recently added a new column called auth_token to my users which is suppose to be generated when the new user is created. 我最近向我的用户添加了一个名为auth_token的新列,假设在创建新用户时生成该列。

How do I add an auth_token to my existing users via migrations? 如何通过迁移向现有用户添加auth_token
Source: http://railscasts.com/episodes/274-remember-me-reset-password 资料来源: http//railscasts.com/episodes/274-remember-me-reset-password

ps. PS。 I cannot find this in http://guides.rubyonrails.org/migrations.html so please don't send me there. 我在http://guides.rubyonrails.org/migrations.html中找不到这个,所以请不要发送给我。

class DoThisThing < ActiveRecord:: Migration
   def self.up
      @users = Users.all
      @users.each do |user|
         user.auth_token = generate_token
         user.save
      end
   end
   def self.down
      puts "non recoverable migration"
   end
   def generate_token
      123456789
   end
end

You can put any standard Ruby/Rails code you like into a migration. 您可以将任何标准的Ruby / Rails代码放入迁移中。 Just be mindful of stdin/out and whether or not any particular libs you'd need are loaded into the memory space of the migration. 请注意stdin / out以及是否需要将任何特定的lib加载到迁移的内存空间中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM