Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

devise and STI how to login as Base class upon registration

I've seen similar posts already, but couldn't quite get the answer I needed.

I have a User model and using STI a Student model that is a type of User.

When I create a new Student, Devise logs in that Student with a student_session. The problem is the rest of my app uses a user_session. SO, should I create a new user_session using the student_session? and then logout the student?

Or is there a way to get Devise to allow a student creation, but login as the User base model?

Thank you, Anthony

like image 377
alassiter Avatar asked Aug 28 '11 23:08

alassiter


1 Answers

Check out this post and see if it helps you:

Rails: Using Devise with single table inheritance

The summary is essentially to do the following:

config/routes.rb:

devise_for :users, :controllers => { :sessions => 'sessions' }, :skip => :registrations
devise_for :students, :skip => :sessions

app/controllers/sessions_controller.rb:

class SessionsController < Devise::SessionsController
    def create
        rtn = super
        sign_in(resource.type.underscore, resource.type.constantize.send(:find, resource.id)) unless resource.type.nil?
        rtn
    end
end
like image 73
abhir Avatar answered Nov 12 '22 17:11

abhir