Note that there are some explanatory texts on larger screens.

plurals
  1. POModelling an application in Rails - mixing STI and polymorphic associations
    primarykey
    data
    text
    <p>Please forgive me if this has been answered already but I've searched a lot and still require some clarification.</p> <p>I'm building a Property Management tool using Rails 3.2. I already have a User model and authorisation/authentication code in place with full tests coverage.</p> <p>I've just started drawing out the models/classes/tables and have gotten myself a bit confused.</p> <p>Let's start with Users.</p> <h2>Modelling Users</h2> <p>I plan to have allow multiple companies to use this system. Each will have employees (users). These users will have different roles e.g. Manager, Agent, Accountant, Secretary etc. For the most part the data I plan to store for each of these users will be similar (or so I think at the moment) so I am leaning towards Single Table Inheritance and using the type to define the level of access each employee has.</p> <p>Secondly, I plan to allow Landlord and Tenants to also log in to the system. Upon logging in they'll be able to view information about the property they are owning or renting - maybe keep their contact details up to date too.</p> <p>I was thinking of using polymorphic associations to represent these users.</p> <p>So the plan I have at the moment and would like some feedback on is to have</p> <p>User &lt; ActiveRecord::BASE</p> <p>Employee &lt; User (this will have a STI type column and allow for the different employee roles)</p> <p>Landlord &lt; User</p> <p>Tenant &lt; User</p> <p>Is this the best way of approaching this problem or am I shooting myself in the foot?</p> <p>I've had some people advise me I should have a 'roles' table and assign roles to the users - but I have a feeling this isn't the most elegant way to do this in Rails.</p> <h2>Properties</h2> <p>My next issue is with Properties. Right now I have a model for properties and when I add them they belong_to a User (i.e. they have a user_id foreign key). I then started thinking "what happens if the employee (user) that added the Property leaves the company or has their account deleted for some reason?"</p> <p>So in this scenario is it best to forgo the User/Employee to Property association and just link the Property to the Company that the employee belongs to? This way I can all employee.company.properties to list out all the properties?</p> <h2>Landlord and Tenant associations</h2> <p>Let's presume we make Properties belong to a Company.</p> <p>In terms of associations this is what I have in my head. Looking at it now I see that everything belongs to a company because one company using the system shouldn't be able to see the landlords/tenants/employees/properties of another company.</p> <pre><code>class Landlord &lt; User belongs_to :company has_many :properties, :through =&gt; :ownerships end class Tenant &lt; User belongs_to :company has_one :property, :through =&gt; tenancies #you can only live at one place at a time right? end class Property &lt; ActiveRecord::Base has_many :tenants, :through =&gt; :tenancies has_many :landlords, :through =&gt; :ownerships belongs_to :company end class Company &lt; ActiveRecord::Base has_many :properties has_many :employees has_many :landlords :through =&gt; :ownerships #not sure if through is required/works here has_many :tenants :through =&gt; :tenancies #not sure if through is required/works here end class Employees &lt; User belongs_to :company end </code></pre> <h2>Properties</h2> <p>Also I'm guessing we'll have different types of Properties (Commercial/Residential) and of those there will be whole buildings, apartments within a building (single address) etc.</p> <p>Like with users I'm planning on using Polymorphic Associations to define two subclasses CommercialProperty and ResidentialProperty and then use sti to define a type. If the type is "multi unit" then have a new model for units and an association whereby Property has_many Units and a Unit belongs_to a property.</p> <h2>Aim</h2> <p>I'm trying to make sure that my code follows best practice as much as possible so that when I come to add new features and expand I'm not left having to re-write large chunks of the app.</p> <p>I would really appreciate your feedback.</p> <h2>Reference</h2> <p>Some of the posts I've read. Hopefully to help others trying to solve the same problem.</p> <p><a href="https://stackoverflow.com/questions/7719600/designing-a-rails-application-single-table-inheritance">Designing a Rails application: single table inheritance?</a></p> <p><a href="https://stackoverflow.com/questions/2586329/ruby-on-rails-with-differnt-user-types">Ruby on rails with different user types</a></p> <p><a href="https://stackoverflow.com/questions/3956588/ruby-on-rails-user-model-for-multiple-types">Ruby On Rails User Model for multiple types</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload