TopperH
TopperH

Reputation: 2223

Devise and a separate users table

I'm working on a rails 3.2 app that authenticates using devise.

For a goal that is not related to the application (mostly statistics) I need to create a separate users table (let's call it alt_users) that only holds some fields of the users table (name, email, and some other fields) but no password digests and other sensitive infos. Also this records don't have to be modified if a user modifies his account or deletes it.

My idea is that when a user signs up, before devise makes his job some selected fields are inserted in the alt_users table.

What is the correct way to override devise behavior in order to make this happen?

Upvotes: 0

Views: 264

Answers (1)

Erez Rabih
Erez Rabih

Reputation: 15788

What you can do is to override Devise's RegistrationsController in the following way:

In your routes.rb:

devise_for :users, :controllers => {:registrations => "registrations"}

Then create the file app/controllers/registrations_controller.rb :

# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController

  def create
    # add your data to alt_users here
    super
  end

end 

On a side note, if you can avoid overriding Devise's controller it would be best. Try to think of other options like a before_create callback on the User model.

Upvotes: 3

Related Questions