Devise and Single Table Inheritance

179 views
Skip to first unread message

Volkan Unsal

unread,
Oct 15, 2010, 4:17:13 PM10/15/10
to Devise
I want to use just one table called Users. Does Devise know what to do
when there is only one table and 3 models? From the small amount of
testing I've done, it doesn't seem to be.

Code Daemon

unread,
Oct 16, 2010, 3:09:35 AM10/16/10
to Devise
Yes, it will just do something like this:


class User < ActiveRecord::Base

has_and_belongs_to_many :roles

# Include default devise modules. Others available are:

# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable
and :oauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,

# Setup accessible (or protected) attributes for your model

attr_accessible :email, :password, :password_confirmation, :remember_me

end
##############################
class OtherUser < User

before_create :setup_role #this is not part of Devise

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable


attr_accessible :email, :password, :password_confirmation, :remember_me

def setup_role
if self.role_ids.empty?
self.role_ids = [6]
end
end

end
##############################
class EndUser < User

before_create :setup_role

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable


attr_accessible :email, :password, :password_confirmation, :remember_me

def setup_role
if self.role_ids.empty?
self.role_ids = [5]
end
end

end

##############################

Depending on what you want to do with those, you'll need to modify
your routes file so everything matches.
For example,

devise_for :end_users, :controllers => { :registrations => "end_users/
registrations", :sessions => "end_users/sessions" }
Reply all
Reply to author
Forward
0 new messages