KhoaVo KhoaVo - 7 months ago 29
Ruby Question

add devise:confirmable to 2 models

Can someone give me an example of how to add devise to 2 different models with existing databases,i have 2 models, customer and vendor.If I just add :confirmable on both models and do the migration

rails g migration add_confirmable_to_devise
will confirmable option be included in both models after I migrate the database?

Answer

No you would have to create two separate migrations:

rails g migration add_devise_fields_to_customer

class AddDeviseFieldsToCustomer < ActiveRecord::Migration
  def change
    # Confirmable columns
    add_column :customers, :confirmation_token, :string
    add_column :customers, :confirmed_at, :datetime
    add_column :customers, :confirmation_sent_at, :datetime
    add_column :customers, :unconfirmed_email, :string
  end
end

rails g migration add_devise_fields_to_vendor

class AddDeviseFieldsToVendor < ActiveRecord::Migration
  def change
    # Confirmable columns
    add_column :vendors, :confirmation_token, :string
    add_column :vendors, :confirmed_at, :datetime
    add_column :vendors, :confirmation_sent_at, :datetime
    add_column :vendors, :unconfirmed_email, :string
  end
end

That was just for Confirmable as that is the module you specified. If you wanted other devise modules (Trackable, DatabaseAuthenticatable etc) you would need to add those columns to the migration too.

You would also have to add the :confirmable (and any other features you wanted) to the models themselves.