Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Single Table Inheritance And where to use it in Rails

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 232
    Comment on it

    This particular post will give you a overview of Single Table Inheritance And where to use it in Rails.

    Single Table Inheritance is, as the name suggested, it is a way to add inheritance to your models. STI lets you to save different models inheriting from the same model inside a single table.

    For example, lets say you have an user model. The users can be of two types : admin or engineer. So they basically share the same attributes and columns. However, their behavior will be different. So creation of two tables having the exact fields would not be a good idea.

    But with the help of STI, you can keep your user model and simply subclass it with your two types of users. You can add a type column to the users table and ActiveRecord will automatically use to identify the sub-model.

    STI should be used if your sub-models will have the same attributes but need different behavior. If you plan to add 12 columns only used by one sub-model, using different tables might be a good choice.

    Lets take a real example to understand this:

    # app/models/tribe.rb
    class Tribe < ActiveRecord::Base 
        has_many :animals 
    end
    
    # app/models/animal.rb
    class Animal < ActiveRecord::Base 
        belongs_to :tribe 
        self.inheritance_column = :race 
    
    # We need a way to know which animals
    # will be the subclass of Animal model
    def self.races
      %w(Horse Owl Butterfly)
    end
    
    end
    
    class Horse < Animal; end  # app/models/horse.rb
    class Butterfly < Animal; end   # app/models/butterfly.rb
    class Owl < Animal; end  # app/models/owl.rb
    

    We setup the relationship between the tribe model and the animals and create 3 empty sub-models. Please note that self.inheritance_column = :race is used to modify to use different column for STI be default it uses column type. and some times in the future if you want to remove the STI or if you want to use the type column for some other purpose you have to mention it like

    self.inheritance_column = :other_column.

    and when you save the data in the tribe

    tribe = Tribe.create(name: 'HorseTribe') 
    

    Create some animals and add them to the tribe

    tribe.animals << Horse.new(name: "Chetak", age: 20) 
    tribe.animals << Butterfly.new(name: "Pum", age: 10) 
    tribe.animals << Owl.new(name: "Tim", age: 10)
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: