# One table, two models

**URL:** <https://www.ruby-forum.com/t/one-table-two-models/63388>\
**Category:** Rails\
**Created:** [June 14, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/one-table-two-models/63388 "2006-06-14T20:55:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [June 14, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/one-table-two-models/63388/1 "2006-06-14T20:55:28Z")

</div>

so i have a people table, and a person has a persontype. the person  
types are customer, and employee. I would like to make them into two  
models, where i can call Employee.new(:name =\> ‘chris’) and it would  
make a person with the name chris and the persontype of employee, and be  
able to do the same thing for Customer. is this possible while still  
using only the one person table?

---

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [June 14, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/one-table-two-models/63388/2 "2006-06-14T21:12:38Z")

</div>

As far as I know, no.

But you can use single table inheritance to handle this

create\_table :people do |t|  
column :first\_name, :string  
column :last\_name, :string  
column :type, :string  
end

class Person \< ActiveRecord::Base  
end

class Employee \< Person  
end

class Customer \< Person  
end

p = Person.create :first\_name =\> “Homer”, :last\_name =\> “Simpson”,  
:type  
=\> “Customer”  
p.class  
=\> Customer

or just do

c = Customer.create :first\_name =\> “Homer”, :last\_name =\> "Simpson

Notes:

The “type” column is absolutely necessary. It stores the class name as a  
string.

All columns used in all models must be in the person table. You may end  
up  
with lots of null columns, but that’s usually ok.
