{"id":115,"date":"2006-06-03T16:29:44","date_gmt":"2006-06-03T21:29:44","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/06\/03\/rails-single-table-inheritance\/"},"modified":"2009-07-20T14:49:43","modified_gmt":"2009-07-20T21:49:43","slug":"rails-single-table-inheritance","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/06\/03\/rails-single-table-inheritance\/","title":{"rendered":"Rails Single Table Inheritance"},"content":{"rendered":"<p>Ruby on Rails supports Single Table Inheritance.  Single Table Inheritance allows you to have one single SQL table, such as an employees table, manage different type of employees like  managers and developers.  A manager would normally have more responsibilities than a developer and you can separate these additional responsibilities in a different ActiveRecord model class.<\/p>\n<p>Here is another example of Single Table Inheritance that you might find useful.  You might have to support a hierarchy of users for you web application, such as User, Editor, and Administrator.  An Administrator might have full Create, Read, Update, and Delete (CRUD) access while a User might just have read access.  For this hierarchy of users you will need to create a Ruby class for each type and place it in the Rails app\/models directory.  Here is the base User class:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nclass User &lt; ActiveRecord::Base\r\nend\r\n<\/pre>\n<p>My Editor class:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nclass Editor &lt; User\r\nend\r\n<\/pre>\n<p>And the Administrator class:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nclass Administrator &lt; Editor\r\nend\r\n<\/pre>\n<p>Place each of these classes in their own Rails model file under the app\/models directory.<\/p>\n<p>To indicate to Ruby on Rails that the users table needs to support Single Table Inheritance you need to add a column named &#8216;type&#8217; to the users table.  Here is my users table definition:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE TABLE users (\r\n   id INT NOT NULL AUTO_INCREMENT,\r\n   user VARCHAR(15) NOT NULL UNIQUE,\r\n   pass VARCHAR(40) NOT NULL,\r\n   type VARCHAR(20) NOT NULL,\r\n   PRIMARY KEY (id)\r\n);\r\n<\/pre>\n<p>In the column named type you should store the name of the class, the class type, that should be used for each user.  To mark an certain user as an admin set his type to &#8216;Administrator&#8217;.  By setting a user&#8217;s type to &#8216;Administrator&#8217; you are giving him full administrator privileges as defined in your Administrator model class.<\/p>\n<p>And now, in your controller you can look up all users that are administrators by using the following bit of code:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nallAdmins = Administrator.find(:all)\r\n<\/pre>\n<p>If you want to look up all known users you can do so by using the User class as in the following snippet of code:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nallUsers = User.find(:all);\r\n<\/pre>\n<p>At this point, you would add additional responsibilities to editors and administrators by adding methods to those model classes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ruby on Rails supports Single Table Inheritance. Single Table Inheritance allows you to have one single SQL table, such as an employees table, manage different type of employees like managers and developers. A manager would normally have more responsibilities than a developer and you can separate these additional responsibilities in a different ActiveRecord model class. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[19,22,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-1R","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/115"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":1,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":778,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/115\/revisions\/778"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}