Single table inheritance
-
Upload
jyaasa-technologies -
Category
Technology
-
view
214 -
download
2
Transcript of Single table inheritance
Single Table Inheritance
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
Hi all, I am Surya Prasad Siwakoti
Associate Software EngineerJyaasa Technologies
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
Objectives● Introduction to STI (Single Table Inheritance)● When to use STI● How to implement STI● When not to use STI● Demo
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
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
class User < ActiveRecord::Base
end
class Stylist < User
end
class Customer < User
end
Object Oriented way instead of Database Relational approach between two objects
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
When to Use STIWhen there are two or more than two objects which have same attributes but different behaviours.
class User < ActiveRecord::Baseend
class Stylist < User def requested_or_verified? profile.requested? || profile.verified? endend
class customer < User def habbit_image
return ‘default-photo’ if habit_image.blank?
endend
Stylist.new({ first_name: “surya”})
Stylist.last.first_name=> “surya”
Customer.new({ first_name: “sarbada”})
Customer.last.first_name=> “sarbada”
Benefits of STI1. 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 gems3. OO way4. Faster query
A single query in one table is enough for retrieving data.3. DRY Principle
● DRY controller● No redundant data in table
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.
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
● 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
Thank you