Q:

How do you get the type when using Table Per Type inheritance in Ruby on Rails?

I have a couple of classes that look like this:
Person {
    id(PK)
    first_name string
    last_name string
}

class Employee {
    person_id(FK)
    job_description string
}

class Student {
    person_id(FK)
    school_name string
}

If I had a large list of People, how could I figure out what type each of them are without having to do
Student.where(person_id = person.id).any?

and
Employee.where(person_id = person.id).any?

for every "person" in the list?
I need to do a similar operation very often, so would Single Table Inheritance be a better choice?

A:

It looks like what you're trying to implement is Class Table Inheritance.
(See earlier questions. eg. "Class Table Inheritance in Rails 3")
There are various gems trying to implement this,
and they each have their own opinions.
Fundamentally if all you know is the "person_id" then you will always need to look up the other tables to find which class it is.
The simplest way you can do this without changing anything fundamental is to use rails' has_one to create a cached relation between the two tables.
class Person
  has_one :employee_details, class_name: "Employee"
  has_one :student_details, class_name: "Student"

  def employee?
    employee_details.present?
  end

  def student?
    student_details.present?
  end
end

Importantly a Person can be both an employee and a student.
If that is not the case, then I suggest you look at Rails' Single Table Inheritance, or consider another way of implementing the same.

