Skip to content

How To: Add :confirmable to Users

tommcfarlin edited this page Feb 16, 2012 · 20 revisions

If you find yourself needing to introduce confirmable to your user model after the application has already been used for sometime, you will end up marking existing users as unconfirmed in the application.

For example, assume that you have a system full of users and you need to implement email notifications for user accounts. In doing this, existing user accounts will be left unconfirmed thus unable to login.

Here's how you can introduce confirmable to users while also marking existing users as confirmed.

First, make sure that you've created a migration to add the proper columns:

class AddConfirmableToUsers < ActiveRecord::Migration
  def self.up
    change_table(:users) do |t|
      t.confirmable
    end
    add_index :users, :confirmation_token, :unique => true
  end

  def self.down
    remove_column :users, :confirmable
  end
end

Next, add :registerable and :confirmable to the User model:

devise :registerable, :confirmable

To confirm all existing user accounts, load up a Rails console:

rails console production

Iterate through each user and set their confirmed_at attribute equal to the current time, then save the user:

User.all.each do |u|
    u.confirmed_at = Time.now.utc
    u.save
end

All existing user accounts should be able to login after this.

Clone this wiki locally