Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3988772
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:17:32+00:00 2026-05-20T06:17:32+00:00

Lets say there is a activerecord class called user, which is representative of user

  • 0

Lets say there is a activerecord class called user, which is representative of user table of database.

But I have different type of users which have

  • special functions
  • special variables
  • custom relations (Employer has_many companies, Employee belongs_to company 🙂

But also these users have a lot of functionality in common. So what I want to do is create classes for each different type of user then inherit them from user class.

  • User < ActiveRecord::Base
  • Employer < User
  • Employee < User
  • Customer < User

What is the best way of doing something like that?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:17:33+00:00Added an answer on May 20, 2026 at 6:17 am

    A lot of applications start out with a User model of some sort. Over time, as different kinds of users emerge, it might make sense to make a greater distinction between them. Admin and Guest classes are introduced, as subclasses of User. Now, the shared behavior can reside in User, and subtype behavior can be pushed down to subclasses. However, all user data can still reside in the users table.

    All you need to do is add a type column to the users table that will hold the name of the class to be instantiated for a given row. Active Record takes care of instantiating the kind of object when it loads it from the database.

    This technique is called Single Table Inheritance or STI (for short).

    A very good recent article about STI is here: http://code.alexreisner.com/articles/single-table-inheritance-in-rails.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have two tables. class CreateUsers < ActiveRecord::Migration def self.up create_table :users
Let's say you have a class with a Uri property. Is there any way
Let's say I have a Base class and several Derived classes. Is there any
Lets say you have a relational DB table like INVENTORY_ITEM. It's generic in the
Lets say I have a website www.mySite.com and there are a lot of pages
Lets say we have a two players game, where one player always wins (there
Let's say I have an ActiveRecord model called Book that has a has_many association
Lets say there was an architecture XYZ which was optimised for functional languages (like
Let's say at your job your boss says, That system over there, which has
Let's say I have a form_tag in a view gathering a view user input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.