Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You've got four basic patterns you can use here that might work.</p> <p><strong>Omnirecord</strong></p> <p>In this model you have all the possible fields in a single record and then use STI to differentiate between profile types. This is the simplest to implement but looks the most messy as few people will have all fields populated. Keep in mind that <code>NULL</code> string fields don't take up a lot of database space, typically one bit per column, so having a lot of them isn't a big deal.</p> <p><strong>Optional Joins</strong></p> <p>In this model you create a number of possible linkages to different profile types, like <code>doctor_profile_id</code> linking to a DoctorProfile, <code>patient_profile_id</code> linking to a PatientProfile, and so forth. Since each relationship is spelled out in a specific field, you can even enforce foreign key constraints if you prefer, and indexing is easy. This can come in handy when a single record requires multiple different profiles to be associated with it, as in the case of a patient that's also a doctor.</p> <p><strong>Polymorphic Join</strong></p> <p>In this model you link to a specific profile type and profile id using the <code>:polymorphic</code> option, much like you've suggested. Indexing is more complicated and foreign keys are impossible. You're also limited to having one and only one profile. These tend to work as a starting point but may prove to be trouble down the road when you get a doctor + patient requirement.</p> <p><strong>Key/Value Store</strong></p> <p>In this model you abandon all efforts to organize things into singular records and instead build an associated ProfileField and ProfileValue table. The ProfileField identifies what fields are available on what kinds of profiles, such as label, allowed values, data type and so forth, while the ProfileValue is used to store specific values for specific profiles.</p> <pre><code>class User &lt; ActiveRecord::Base has_many :profile_fields end class ProfileField &lt; ActiveRecord::Base has_many :profile_values end class ProfileValue &lt; ActiveRecord::Base belongs_to :user belongs_to :profile_field end </code></pre> <p>Since this one is wide open you can allow the site administrator to redefine what field are required, add new fields, and so on, without having to make a schema change.</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.
    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.
 

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