Single Table Inheritance (STI) is defined as multiple subclasses sharing a single database table. Rails supports STI right out of the box simply by inheriting subclasses from an ActiveRecord parent class which has a type column in its table.
Tutorial from: https://www.driftingruby.com/episodes/single-table-inheritance
- Ruby 2.6.3
- Rails 5.2.4
- PostgreSQL 11.5
Edit the migration like below
..._create_contacts.rb
class CreateContacts < ActiveRecord::Migration[5.2]
def change
create_table :contacts do |t|
t.integer :user_id
t.string :type
t.string :first_name
t.string :last_name
t.string :phone_number
t.string :address
t.string :city
t.string :state
t.date :birthday
t.timestamps
end
add_index :contacts, [:type, :user_id]
end
end
user.rb
class User < ApplicationRecord
has_many :emergencies, class_name: 'Emergency'
has_many :friends, class_name: 'Friend'
end
profile.rb
class Contact < ApplicationRecord
scope :friends, -> { where(type: 'Friend') } # Contact.friends
scope :emergency, -> { where(type: 'Emergency') } # Contact.emergencies
end
friend.rb*
class Friend < Contact
belongs_to :user
end
emergency.rb
class Emergency < Contact
belongs_to :user
end