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?
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.
精彩评论