# Authlogic and Single table inheritance

**URL:** <https://www.ruby-forum.com/t/authlogic-and-single-table-inheritance/188968>\
**Category:** Rails\
**Created:** [May 21, 2010, 7:45am UTC](https://www.ruby-forum.com/t/authlogic-and-single-table-inheritance/188968 "2010-05-21T07:45:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [May 21, 2010, 7:45am UTC](https://www.ruby-forum.com/t/authlogic-and-single-table-inheritance/188968/1 "2010-05-21T07:45:53Z")

</div>

Hi,

I have some models such as:

```
class User < ActiveRecord::Base
  acts_as_authentic
end

class Admin < User
end

class Superadmin < Admin
end

```

And some controllers such as:

```
class UserSessionsController < ApplicationController
  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      flash[:notice] = t('user_sessions.flash.create.notice')
      redirect_to root_path
    else
      render :action => :new
    end
  end

  def destroy
    current_user_session.destroy
    flash[:notice] = t('user_sessions.flash.destroy.notice')
    redirect_to new_session_path
  end
end

class Admin::AdminSessionsController < ApplicationController
  layout 'admin'

  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      flash[:notice] = t('user_sessions.flash.create.notice')
      redirect_to admin_dashboard_path
    else
      render :action => :new
    end
  end

  def destroy
    current_user_session.destroy
    flash[:notice] = t('user_sessions.flash.destroy.notice')
    redirect_to new_admin_session_path
  end
end

class Superadmin::SuperadminSessionsController <

```

ApplicationController  
layout ‘superadmin’

```
  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      flash[:notice] = t('user_sessions.flash.create.notice')
      redirect_to superadmin_dashboard_path
    else
      render :action => :new
    end
  end

  def destroy
    current_user_session.destroy
    flash[:notice] = t('user_sessions.flash.destroy.notice')
    redirect_to new_superadmin_session_path
  end
end

```

I would like to allow users to login, but only with the good controller  
and model.  
For instance, if an admin want to log in, its controller should be  
Admin::AdminSessionsController (handled Admin model).

But I don’t want that, a user connect itself using another interface  
like superadmin (and the opposite).

I suppose that I need to add a condition in each controller just before  
@user\_session.save. Maybe using @user\_session.save… But I don’t see  
exactly how.

I just know that, it’s should be possible to know the type of  
current\_user thanks to:  
current\_user.type (as documented at  
[ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html))

According to you, how can we do so?

Many thanks.
