Single table inheritance

Jyaasa 601 views 15 slides Jun 27, 2016
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation

Single table Inheritance


Slide Content

Single Table Inheritance http://jyaasa.com Copyright 201 6 . Jyaasa Technologies.

Hi all, I am Surya Prasad Siwakoti Associate Software Engineer Jyaasa Technologies http://jyaasa.com Copyright 201 6 . 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 STI When there are two or more than two objects which have same attributes but different behaviours.

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”

Benefits of STI 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

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