Created
April 4, 2014 01:59
-
-
Save to4iki/9966636 to your computer and use it in GitHub Desktop.
存在するテーブルにカラム追加 + 値代入
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddCodeToUsers < ActiveRecord::Migration | |
def change | |
add_column :users, :code, :string, limit: 8, after: :support_code, comment: 'コード' | |
User.reset_column_information | |
reversible do |dir| | |
dir.up do | |
# 衝突するかしないかは運任せな所あり | |
User.find_each do |u| | |
u.update(invited_code: SecureRandom.hex(4).upcase) | |
end | |
end | |
end | |
add_index :users, :invited_code, unique: true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment