Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add devise, if user model already exists?

We already created a user model in the beggining of the project, but now (several migrations later) we would like to use the devise gem. Is it possible to add devise if the user model and table already exist? That is, is it possible to alter what is already done, or do we have to start all over again?

like image 594
sara Avatar asked Oct 22 '10 22:10

sara


People also ask

How do I add devise to an existing model?

Add Devise for existing model First, add a column called "email" if you don't already have one. Second, make sure that every existing row has a unique value for email. Finally, go into the migration file called "add_devise_to_users" and comment out the line that adds an email column. and restart your server.

How does devise Current_user work?

current_user is a Devise helper that accesses the details of the user who is currently signed in to the application. For instance, if you sign in with [email protected] , the current_user helper would return the user model for [email protected] . So, when using current_user.

How do I add a column to a devise user?

When you create a User model with Devise using the command rails generate devise User . Devise will create default columns in the database for your User model. To add your own columns for your users you can create a new Rails migration and add any columns you want for your users.


1 Answers

Cavert Coder, but:

(Note, this doesn't migrate ":lockable" because I didn't care about it when I wrote it This now includes :lockable because MattSlay cared more than I did :). Also, you'll need to migrate your users passwords into the encrypted passwords field. Finally, it might not work for you. Sorry.)

class AddDevise < ActiveRecord::Migration
  def self.up
    null    = false
    default = ""

    add_column :users, :encrypted_password, :string, :null => null, :default => default, :limit => 128
    add_column :users, :password_salt, :string
    add_column :users, :authentication_token, :string
    add_column :users, :confirmation_token,   :string
    add_column :users, :confirmed_at,         :datetime
    add_column :users, :confirmation_sent_at, :datetime
    add_column :users, :reset_password_token, :string
    add_column :users, :remember_token,      :string
    add_column :users, :remember_created_at, :datetime
    add_column :users, :sign_in_count,      :integer, :default => 0
    add_column :users, :current_sign_in_at, :datetime
    add_column :users, :last_sign_in_at,    :datetime
    add_column :users, :current_sign_in_ip, :string
    add_column :users, :last_sign_in_ip,    :string

    #:lockable fields contributed by MattSlay
    add_column :users, :failed_attempts, :integer, :default => 0
    add_column :users, :unlock_token,   :string
    add_column :users, :locked_at, :datetime

  end

  def self.down
    remove_column :users, :encrypted_password
    remove_column :users, :password_salt
    remove_column :users, :authentication_token
    remove_column :users, :confirmation_token
    remove_column :users, :confirmed_at
    remove_column :users, :confirmation_sent_at
    remove_column :users, :reset_password_token
    remove_column :users, :remember_token
    remove_column :users, :remember_created_at
    remove_column :users, :sign_in_count
    remove_column :users, :current_sign_in_at
    remove_column :users, :last_sign_in_at
    remove_column :users, :current_sign_in_ip
    remove_column :users, :last_sign_in_ip
    remove_column :users, :failed_attempts
    remove_column :users, :unlock_token
    remove_column :users, :locked_at
  end
end
like image 88
Aquarion Avatar answered Sep 25 '22 21:09

Aquarion