开发者

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

开发者 https://www.devze.com 2023-03-23 00:53 出处:网络
I recently added a new column called auth_token to my users which is suppose to be generated when the new user is created.

I recently added a new column called auth_token to my users which is suppose to be generated when the new user is created.

How do I add an auth_token to my existing users v开发者_StackOverflow中文版ia migrations?

Source: http://railscasts.com/episodes/274-remember-me-reset-password

ps. I cannot find this in http://guides.rubyonrails.org/migrations.html so please don't send me there.


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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消