Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>STI is probably a good fit for your requirements, as suggested by tadman, if you are using ActiveRecord (from Rails 3, it is easy to change ORM). The basic information is available on the <a href="http://ar.rubyonrails.org/classes/ActiveRecord/Base.html" rel="nofollow noreferrer">AR documentation page</a>, but here is some extra information w.r.t. your target:</p> <ul> <li>Define one model per file. Otherwise there are some initialization troubles. Assuming Client inherits from User all in a single file, Client objects cannot be created as long as a User constructor has not been called once. One file per model circumvents the problem.</li> <li>All attributes through the hierarchy are defined one-shot in the top class. This is for performance issues, but it seems disturbing many people in blog posts. In short, the Ruby code is object-oriented and encapsulates properly the attributes. The DB contains everything in a single table, with the extra "type" column to distinguish where they belong in the hierarchy. This is only a "trick" to represent inheritance trees in relational databases. We must be aware that the ORM mapping is not straightforward. The <a href="http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html" rel="nofollow noreferrer">image on this site from Martin Fowler</a> may help understand the situation.</li> </ul> <p>In addition, you would like any new user to be a client, where Client inherits from User. To do so, you may simply instantiate any new user as a client. In your controller for creating users:</p> <pre><code>user = Client.new # Do something to user user.save #=&gt; &lt;Client id: 1, name: "Michael Bolton", email: "mike@bolton.net", created_at: "2010-05-30 03:27:39", updated_at: "2010-05-30 03:27:39"&gt; </code></pre> <p>All the above is still valid with Rails 3 when using ActiveRecords.</p>
 

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