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
$ rails g migration add_type_to_users type:string
# db/migrations/20200221021551_add_type_to_users.rb
class AddTypeToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :type, :string
end
end
user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
participant.rb
class Participant < User
end
sponsor.rb*
class Sponsor < User
end
# config/routes.rb
Rails.application.routes.draw do
root to: 'pages#index'
devise_for :participants
devise_for :sponsors
namespace :dashboard do
root to: 'dashboard#index'
end
end