Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, let's establish what single-table inheritance typically is used for. It is a way to combine the storage and behaviour of multiple things that resemble each other. Sticking to a CMS, an example would be a table with <code>posts</code>, which could be either a <code>Comment</code> or an <code>Article</code>. They share similar data and behavior, but are ultimately different things. Whether or not something is a comment is <strong>not the state of the object</strong>, it's an identity.</p> <p>In your example, however, whether or not a page is public or private, shared or not, or hidden, appears to be a part of the <strong>state</strong> of the page. Although single-table inheritance might technically work (provided all subclasses are mutually exclusive), it's not a good fit.</p> <p>State should be implemented in one or more <strong>columns</strong>. An attribute that represents a certain dual state can be specified as a boolean; <em>yes</em> or <em>no</em>. If a page always is <strong>either private or public</strong>, you can model this as a single boolean column, <code>private</code>. If it's not private it's public (or the other way around).</p> <p>In some cases you may want to store three or more different states that are mutually exclusive. For example, a page could be <strong>either private, or public, or shared</strong> (I don't know if this is the case -- let's pretend that it is). In this case a boolean will not help. You could use multiple boolean flags, but as you correctly observe that is very confusing. The easiest way is to model this as an <a href="http://en.wikipedia.org/wiki/Enumeration" rel="nofollow"><em>enumeration</em></a>. Or when you lack this (as is the case with Rails), simply use string values with a special meaning and add a validation that ensures the only values you use are one of <code>private</code>, <code>public</code> or <code>shared</code>.</p> <p>Sometimes certain combinations of different state variables are <em>invalid</em>. For example, a page might be a <em>draft</em> or <em>approved</em> (reflected by a boolean column <code>approved</code>); and it is also either <em>public</em> or <em>private</em> (also reflected by a boolean column). We could decide that a page should <em>must</em> be approved before it is made public. In this case we declare one of the states invalid. This should be reflected by the <strong>validation of your model</strong>. It is important to realise that a <em>draft</em>, <em>public</em> page is not <em>fundamentally</em> impossible, it's only impossible because you decide it should not happen.</p> <p>When creating your model, make a careful distinction between the attributes that reflect <strong>actual properties and states</strong> of the subjects in the real world, and the <strong>business rules</strong> that determine what should be possible and what shouldn't be. The first should be modelled as columns, the second as validations.</p> <hr> <p><em>Original answer:</em></p> <p>One obvious difference is that boolean flags allow a <code>Person</code> to be marked as president <strong>and</strong> guard at the same time. If your model should allow these situations, single-table inheritance will not work for you.</p> <p>On the other hand, maybe a <code>Person</code> that is a president <strong>behaves</strong> differently from a regular person; and a single person can <strong>only</strong> be president <strong>or</strong> guard. In this case inheritance may be a better fit. I don't think you should model "part time" as a subclass, though. That is an attribute in any case.</p> <p>There is also an important third option, one where you completely separate the <strong>job</strong> or <strong>role</strong> of a person from the model. One <strong>person</strong> has one (or many?) <strong>jobs</strong>, which are or are not part-time. The advantage of this model is that you separate attributes of a person from the attributes of their job. After all, people change jobs, but that does not make them literally a different person. Ultimately this seems to me the most realistic way to model your situation.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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