Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add columns to devise after the initial create_table rake?

I installed Devise, raked, and then realized afterwards, that I want to add :confirmable.

Can I go back to the same initial migration and just uncomment out the helper that I want and then rake db:migrate again?

I tried it and it didn't seem to work. But I haven't seen an example of how to create a follow-on migration.

Thanks!

This is what I tried:

  1 class AddConfirmableToUsers < ActiveRecord::Migration 
  2   def self.up 
  3     change_table :users do |t| 
  4       t.confirmable 
  5     end 
  6     add_index :users, :confirmation_token,   :unique => true  
  7   end 
  8    
  9   def self.down 
 10     remove_column :users, :confirmation_token 
 11   end 
 12   
 13 end 
like image 514
Satchel Avatar asked Oct 10 '22 18:10

Satchel


1 Answers

You can add the proper columns yourself like so:

class AddConfirmableToUsers < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      t.string :confirmation_token
      t.datetime :confirmed_at
      t.datetime :confirmation_sent_at
    end

    add_index :users, :confirmation_token, :unique => true
  end

  def self.down
    change_table :users do |t|
      t.remove :confirmation_token, :confirmed_at, :confirmation_sent_at
    end

    remove_index :users, :confirmation_token
  end
end
like image 116
dpb Avatar answered Nov 24 '22 09:11

dpb