6

I have a base table called users which holds all common information about a user such as name, address, phone number...etc

I have another table called clients which holds specific information about a client (such as the client's company name and their url) and inherits user information from the users table. A client has a foreign key user_id which maps back to the information about a user.

I have another table called client_admins which hold specific information about client_admins and also has a user_id field AND a client_id field (which links to the clients table).

I have another table called super_admins which links to the users table and has specific information about a Super admin.

I know I could probably get away with Single Table Inheritance as there is not a lot of different data between each of the types, just different functionality and privileges.

What is the best way to model this in Rails 3?

Chris Muench
  • 17,444
  • 70
  • 209
  • 362

2 Answers2

2

Inside your user model:

has_one :client
has_one :client_admin
has_one :super_admin

Inside your client model:

belongs_to :user
has_one :client_admin

Inside your client_admin model:

belongs_to :user
belongs_to :client

Inside your super_admin model:

belongs_to :user
Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158
  • THANK YOU! Is it ok to have a has_one relationship that is optional as in the case of the user model? – Chris Muench Feb 22 '11 at 00:31
  • You mean, like you just want to put belongs_to in the model that contains the foreign key and leaving the has_one part from the other model? – Amokrane Chentir Feb 22 '11 at 00:35
  • Well I am just saying that a User will only relate to either a client, client_admin OR super_admin. So does the user model care that it doesn't have all the relationships? – Chris Muench Feb 22 '11 at 18:02
  • I have read that a couple of times, I think I got the relationships right. You can think of my relationship as a simple table inheritance model. A Patient, Client, SuperAdmin, and ClientAdmin all inherit from the users table for their similar information and the "sub" tables have specific information for each sub type. Does it at least make sense from a database perspective? – Chris Muench Feb 23 '11 at 14:19
  • @Chris: Of course it makes sense! That's exactly how it should be done! – Amokrane Chentir Feb 23 '11 at 18:38
0

I am unsure of whether this is exactly what you were aiming for, but with my citier gem you could do this:

class User < ActiveRecord::Base
   acts_as_citier
   # User methods and validation, inherited by all children (so client & super admins)
   # Useful for things like validating user permissions etc
end

class Admin < User
   acts_as_citier
   # Admin specific methods and validation, inherited by all children
end

class Client < ActiveRecord::Base
end

class ClientAdmin < Admin
   acts_as_citier
   belongs_to :client
   # Client admin specific methods and validation
end

class SuperAdmin < Admin
   acts_as_citier
   # Super admin specific methods and validation
end

Allows each model to have unique fields so better than STI, but also shares methods and properties like normal inheritance.

Check it out - http://peterhamilton.github.com/citier/

Pete Hamilton
  • 7,730
  • 6
  • 33
  • 58