views:

37

answers:

2

Hi, I'm in the process of learning rails. I've found Devise to be great with getting authentication up and running quickly and seamlessly but I do have one question.

How do I change the modules after the first run of the Devise generator (e.g. rails g devise User)? This defaults with the following migration:

def self.up
  create_table(:users) do |t|
    t.database_authenticatable :null => false
    t.recoverable
    t.rememberable
    t.trackable

    # t.confirmable
    # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
    # t.token_authenticatable

    t.timestamps
  end

  add_index :users, :email,                :unique => true
  add_index :users, :reset_password_token, :unique => true
  # add_index :users, :confirmation_token,   :unique => true
  # add_index :users, :unlock_token,         :unique => true
end

If I've run this migration, how do I add/remove some of those modules at a later stage? E.g. Maybe I want to add lockable to an existing User model. I understand how to make the changes in the model and devise.rb but I'm not sure what to do with the migrations.

Apologies if the answer is here already, I've trawled for a couple of hours here and in google and couldn't find anything. Maybe I'm searching for the wrong thing.

Thanks in advance!
Jason
ps. I'm using
rails 3.0.0
devise 1.1.3

A: 

Change the lines you want in the migration file, then redo the migration as per these instructions:

http://guides.rubyonrails.org/migrations.html

Chuck Vose
Hi Chuck. Thanks for the tip. I'll give it a go. Is this approach healthy if there are other migrations after the one used for Devise?
Jason
Yes. Rails is smart enough to apply the needed migrations in the order they were added, based on timestamps and internal tracking of which have already run.
Joost Schuur
Thanks Joost. Wasn't sure.
Jason
A: 

As long as you're just removing options where the appropriate fields were already added to your schema (like confirmable), you can always just edit the Users model directly without a new migration.

Joost Schuur
Thanks Joost. I'm also interested in adding items, e.g. t.lockable is not just a field called lockable.
Jason
I think custom types like this you can't do fully from the rails command line, so you'd be able to run 'rails generate migration AddLockableToUsers' and then manually edit the file in the db/migrate folder it generates and add t.confirmable. Then run 'rake db:migrate' and your field is added.
Joost Schuur