Sha256: ed4606e5ed96c7a4a92f7b189ea25100269477d428864d34982b972a3efbdba7

Contents?: true

Size: 335 Bytes

Versions: 4

Compression:

Stored size: 335 Bytes

Contents

class AddApiKeyToUsers < ActiveRecord::Migration
  def change
    add_column :users, :api_key, :string, limit: 32
    
    User.where('api_key IS NULL').find_each do |user|
      begin
        user.api_key = SecureRandom.uuid.tr('-', '')
      end while User.where(api_key: user.api_key).any?
      
      user.save!
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
voluntary-0.6.0 db/migrate/20150808155719_add_api_key_to_users.rb
voluntary-0.5.2 db/migrate/20150808155719_add_api_key_to_users.rb
voluntary-0.5.1 db/migrate/20150808155719_add_api_key_to_users.rb
voluntary-0.5.0 db/migrate/20150808155719_add_api_key_to_users.rb