Giter Club home page Giter Club logo

strong_sample_01's Introduction

Single Table Inheritance with Devise (Coba)

What is Single Table Inheritance?

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.

(source: https://medium.com/@User3141592/when-to-use-single-table-inheritance-vs-multiple-table-inheritance-db7e9733ae2e)

Tutorial from: https://www.driftingruby.com/episodes/single-table-inheritance

Prerequisite

  1. Ruby 2.6.3
  2. Rails 5.2.4
  3. PostgreSQL 11.5

Migration

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

Models

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

Routes

# 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

strong_sample_01's People

Contributors

bandithijo avatar

Stargazers

 avatar Roman avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.