Topic: STI, Polymorphic Inheritance or other?

Hi all,

I'm planning an application at the moment for my property management business.

I'm looking to develop the tool to manage rentals/lettings of properties but may looking to allow the tool to be used by friends running their own companies too.

So my users are:

Employees (those who work with me)
They have different types: Manager, Agent, Accounting Staff, Administration Staff/Secretaries etc.

Landlords (those who own properties)
Will log in to see information about the properties I'm managing for them

Tenants (those who rent properties)
Will log in to see information about the property they live in (i.e. move in date, contracts etc).

What is the best way of modelling this?

I already have a User model/class/table built with all my authentication/authorisation code and I'm trying to be DRY. So I think it's overkill to re-do this code for each user.

I was thinking of having Employee, Landlord and Tenant as subclasses of User (maybe use polymorphic association - not sure if its the best approach). Also I'd have STI and use the type field on the Employee model to signify which type of staff is logging in and using the system.

Further to this I also have a Property model. At present Properties belong to a User (the user_id is the foreign key in the Property table). I was thinking "what happens if an employee leaves and we delete their ID?)

So maybe it's better to say the Property belongs to a Company. Where the company is that which the current user belongs to. Is this a good approach?

Properties also belong to Landlords or should I be thinking of the Landlords belonging to the Property?

I just want to make sure that I build following best practises and build a tool that will grow with my business as well as cater for others.

Many thanks for taking the time to read and I look forward to your thoughts smile

Oh and I've outlined this problem on stackoverflow, perhaps with a bit more detail and some pseudo code here: … sociations

Last edited by chigzy (2013-02-03 10:30:27)