2. Hi all,
I am Surya Prasad Siwakoti
Associate Software Engineer
Jyaasa Technologies
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
3. Objectives
● Introduction to STI (Single Table Inheritance)
● When to use STI
● How to implement STI
● When not to use STI
● Demo
4. Why Single Table Inheritance
● Avoid Data Redundancy (DRY principle)
● Object Oriented way instead of Database
Relational approach between two objects
● Single table for multiple model
5. create_table :stylist do
|t|
t.id :integer
t.date :date_of_birth
t.string :first_name
t.string :last_name
t.string :email
t.sign_in_count
:integer
t.string :password
t.string :gender
end
Data Redundancy
create_table :customer do |t|
t.id :integer
t.date :date_of_birth
t.string :first_name
t.string :last_name
t.string :email
t.sign_in_count :integer
t.string :password
t.string :gender
end
6. class User < ActiveRecord::Base
end
class Stylist < User
end
class Customer < User
end
Object Oriented way instead of Database
Relational approach between two objects
7. What is Single Table Inheritance?
● STI allows you to create subclasses of a particular database
table.
● Eg: Parent class: user, Child class: stylist, customer
● Using a single table for multiple objects
8. When to Use STI
When there are two or more than two objects which have
same attributes but different behaviours.
9. class User < ActiveRecord::Base
end
class Stylist < User
def requested_or_verified?
profile.requested? || profile.verified?
end
end
class customer < User
def habbit_image
return ‘default-photo’ if
habit_image.blank?
end
end
Stylist.new({
first_name: “surya”
})
Stylist.last.first_name
=> “surya”
Customer.new({
first_name: “sarbada”
})
Customer.last.first_name
=> “sarbada”
10. Benefits of STI
1. Simple approach
● Create a model class eg: stylist
● Inherit from a user
● Create a type field with string data type in user table
2. No more gems
3. OO way
4. Faster query
A single query in one table is enough for retrieving data.
3. DRY Principle
● DRY controller
● No redundant data in table
11. Drawbacks Of STI
● Tightly coupled parent model
● Changes to the database affects controller and view codes.
● Child class database field should not have too many unique attributes.
● We cannot change object class in runtime. An object life cycle should
finish.
a = Stylist.first
a.type = “Customer”
a.class.name
a.reload!
● Forces to save nullable/empty columns.
12. a.update_attributes(type:
"ShippingAddress", country: "Spain") #
=> true # but should be false
a.class.name # => "BillingAddress" #
But we wanted ShippingAddress
a.valid? # => true # but should be
false, we ship only to USA and Canada
class Address < ActiveRecord::Base
validates_presence_of :full_name,
:city, :street, :postal_code
end
class BillingAddress < Address
validates_presence_of :country
end
class ShippingAddress < Address
validates_inclusion_of :country,
in: %w(USA Canada)
end
Type Change Drawback
13. ● Arkency Blog. 2016. Single Table Inheritance - Problems and solutions - Arkency
Blog. [ONLINE] Available at: http://blog.arkency.com/2013/07/sti/. [Accessed 20
June 2016].
● FutureLearn. 2016. Refactoring our Rails app out of single-table inheritance.
[ONLINE] Available at: https://about.futurelearn.com/blog/refactoring-rails-sti/.
[Accessed 20 June 2016].
● Eugene Wang. 2016. How (and When) to Use Single Table Inheritance in Rails -
eugenius. [ONLINE] Available at: http://eewang.github.io/blog/2013/03/12/how-and-
when-to-use-single-table-inheritance-in-rails/. [Accessed 20 June 2016].
References