This is called STI (Single Table Inheritance). The correct way to implement it is to add a column named type to the base class table. Rails will use this column to know what class to instantiate for a given record.
For example lets assume we have three classes, Person, Teacher and Student. Person is the base class and Teacher and Student inherits from it. In this case we implement it as follows:
Migration:
class CreatePeople < ActiveRecord::Migration
def change
create_table :people do |t|
... # person attributes name, birthday ...
... # Student specific attributes
... # Teacher specific attributes
t.string :type # this will be Student, Teacher or Even Person.
t.timestamps null: false
end
end
end
Models
# Person
class Person < ActiveRecord::Base
end
# Teacher
class Teacher < Person
end
# Student
class Student < Person
end
so if you create a new student
student = Student.new(name: 'studentx',...)
student.save
than get it throught the Person class
person = Person.first
puts person.type # => Student
When you create a new student, and to be sure that its attributes are set correctly, so that for example, a student does not have teacher's specific attributes set. you just filter them in the controller through strong parameters( assuming this is a Rails application)