# AR: Many models on one table

**URL:** <https://www.ruby-forum.com/t/ar-many-models-on-one-table/143741>\
**Category:** Rails\
**Created:** [July 22, 2008, 11:56am UTC](https://www.ruby-forum.com/t/ar-many-models-on-one-table/143741 "2008-07-22T11:56:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a88e57/32.png) [@bones](https://www.ruby-forum.com/u/bones)\
**Post date:** [July 22, 2008, 11:56am UTC](https://www.ruby-forum.com/t/ar-many-models-on-one-table/143741/1 "2008-07-22T11:56:37Z")

</div>

Let’s say I have this table:

CREATE TABLE people (  
id serial unique,  
person\_type varchar(32) check type in (‘staff’, ‘customer’),  
name varchar(256)  
);

Now, since I store staff and customers in the same table I’d like to  
have different models for them, too, like:

class Friend \< ActiveRecord::Base  
set\_table\_name ‘people’  
end  
class Staff \< ActiveRecord::Base  
set\_table\_name ‘people’  
end

I understand that I can use before\_insert and before\_update to force a  
person\_type to be ‘staff’ or ‘customer’. But is there also any way to  
add a condition (person\_type=‘staff’) on all the other methods  
especially those who will use SQL SELECT, like find…

Thanks,  
martin

---

<div class="post-metadata">

**Author:** ![bones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a88e57/32.png) [@bones](https://www.ruby-forum.com/u/bones)\
**Post date:** [July 22, 2008, 11:57am UTC](https://www.ruby-forum.com/t/ar-many-models-on-one-table/143741/2 "2008-07-22T11:57:12Z")

</div>

On Jul 22, 9:00 am, bones [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Let’s say I have this table:
> 
> I understand that I can use before\_insert and before\_update to force a  
> person\_type to be ‘staff’ or ‘customer’. But is there also any way to  
> add a condition (person\_type=‘staff’) on all the other methods  
> especially those who will use SQL SELECT, like find…

Sounds like you reinventing STI (single table inheritance)

Fred

---

<div class="post-metadata">

**Author:** ![bones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a88e57/32.png) [@bones](https://www.ruby-forum.com/u/bones)\
**Post date:** [July 22, 2008, 12:10pm UTC](https://www.ruby-forum.com/t/ar-many-models-on-one-table/143741/3 "2008-07-22T12:10:44Z")

</div>

Yes, that will do. Thanks!

On Jul 22, 10:56 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
