Single table inheritance

15
Single Table Inheritance http://jyaasa.com Copyright 2016. Jyaasa Technologies.

Transcript of Single table inheritance

Page 1: Single table inheritance

Single Table Inheritance

http://jyaasa.comCopyright 2016. Jyaasa Technologies.

Page 2: Single table inheritance

Hi all, I am Surya Prasad Siwakoti

Associate Software EngineerJyaasa Technologies

http://jyaasa.comCopyright 2016. Jyaasa Technologies.

Page 3: Single table inheritance

Objectives● Introduction to STI (Single Table Inheritance)● When to use STI● How to implement STI● When not to use STI● Demo

Page 4: Single table inheritance

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

Page 5: Single table inheritance

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

Page 6: Single table inheritance

class User < ActiveRecord::Base

end

class Stylist < User

end

class Customer < User

end

Object Oriented way instead of Database Relational approach between two objects

Page 7: Single table inheritance

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

Page 8: Single table inheritance

When to Use STIWhen there are two or more than two objects which have same attributes but different behaviours.

Page 9: Single table inheritance

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”

Page 10: Single table inheritance

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

Page 11: Single table inheritance

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.

Page 12: Single table inheritance

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

Page 13: Single table inheritance

● 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

Page 14: Single table inheritance
Page 15: Single table inheritance

Thank you