Search code examples
ruby-on-railsinheritancesingle-table-inheritance

Ruby On Rails Hierarchical Relationship Modeling


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?


Solution

  • 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