Skip to content

Instantly share code, notes, and snippets.

@to4iki
Created April 4, 2014 01:59
Show Gist options
  • Save to4iki/9966636 to your computer and use it in GitHub Desktop.
Save to4iki/9966636 to your computer and use it in GitHub Desktop.
存在するテーブルにカラム追加 + 値代入
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