Friday 26 December 2014

Example of Single Table Inheritance in Rails 4



Single-table inheritance (STI) is the practice of storing multiple types of values in the same table, where each record includes a field indicating its :type , and the table includes a column for every field of all the types it stores. In Rails, the type column is used to determine which type of model to instantiate for each row; a row with type = 'Article' will make Rails call Article.new when turning that row into an object.

create_table 'employees' do |t|
  t.string  'type'
  t.string  'name'
  t.text    'email'
end

When we went with this design in the first place. After all, articles, videos, discussions and quizzes don’t seem like subtypes of one another. All these types of content have to be put into a sequence to make a course. So, each content item belongs to a course, and we use acts_as_list to keep the items, or ‘steps’, in order. This is most easily realised if all the steps live in the same table.

class Course < ActiveRecord::Base
  has_many :steps
end

class Step < ActiveRecord::Base
  belongs_to :course
  acts_as_list scope: :course
end

Schema of this model

create_table 'courses' do |t|
  t.string 'title'
end

create_table 'steps' do |t|
  t.string  'type'
  t.integer 'course_id'
  t.integer 'position',   null: false
  t.string  'title'
  t.text    'body'
  t.integer 'asset_id'
  t.string  'url'
  t.string  'copyright'
end

Then we have the content types themselves, for example Article hasbody and copyright attributes, Video has asset_id and copyright, andExercise has body and url.
class Article < Step
  validates :body,      presence: true
  validates :copyright, length: { maximum: 255 }
end

class Video < Step
  validates :asset_id,  presence: true
  validates :copyright, length: { maximum: 255 }
end

class Exercise < Step
  validates :body, :url, presence: true
end


STI should be used if your submodels will share the same attributes but need different behavior. If you plan to add 10 columns only used by one submodel, using different tables might be a better solution.

No comments:

Salesforce CRM vs. Zoho: A Comparative Analysis

Introduction: Selecting the right customer relationship management (CRM) software is crucial for businesses seeking to streamline their sal...