# Single table inheritance

**URL:** <https://www.ruby-forum.com/t/single-table-inheritance/97414>\
**Category:** Rails\
**Created:** [July 6, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/single-table-inheritance/97414 "2006-07-06T22:17:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pratik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4bbf92/32.png) [@pratik](https://www.ruby-forum.com/u/pratik)\
**Post date:** [July 6, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/single-table-inheritance/97414/1 "2006-07-06T22:17:13Z")

</div>

I’m having a class called Person, with subclasses as goalkeeper,  
forward, defender.

Now a person can be a forward as well as a defender or a goalkeeper.

I want to use single table inheritance like :

class Goalkeepr \< Person  
end

And not have multiple boolean columns like in my people table like is  
is\_goalkpeer, is\_forward, is\_defender.

How do I go about it ?

## Thanks, Pratik

rm -rf / 2\>/dev/null - [http://null.in](http://null.in)

“Things do not happen. Things are made to happen.” - JFK

---

<div class="post-metadata">

**Author:** ![pratik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4bbf92/32.png) [@pratik](https://www.ruby-forum.com/u/pratik)\
**Post date:** [July 6, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/single-table-inheritance/97414/2 "2006-07-06T22:42:01Z")

</div>

If you create a column ‘type’ in your table, ActiveRecord should  
magically make STI work.

Tom

---

<div class="post-metadata">

**Author:** ![pratik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4bbf92/32.png) [@pratik](https://www.ruby-forum.com/u/pratik)\
**Post date:** [July 6, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/single-table-inheritance/97414/3 "2006-07-06T22:48:32Z")

</div>

Right. I understand that. But in case of ‘type’ driven STI, a person  
can just have one type. I don’t want to use HABTM with roles because I  
want to seperate logic for different type of people.

I’m looking for something that lets me scope the inherited table  
instead of using type field. Something like :

class Goalkeeper \< Person  
:scope =\> “type\_id = 4”  
end

Probably something like  
[http://roman2k.free.fr/rails/meantime\_filter/0.1.0/rdoc/](http://roman2k.free.fr/rails/meantime_filter/0.1.0/rdoc/) for models.

Thanks,  
Pratik

On 7/6/06, Tom W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Thanks,
> 
> –  
> email : tom at [popdog.net](http://popdog.net)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
rm -rf / 2\>/dev/null - [http://null.in](http://null.in)

“Things do not happen. Things are made to happen.” - JFK

---

<div class="post-metadata">

**Author:** ![pratik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4bbf92/32.png) [@pratik](https://www.ruby-forum.com/u/pratik)\
**Post date:** [July 6, 2006, 10:51pm UTC](https://www.ruby-forum.com/t/single-table-inheritance/97414/4 "2006-07-06T22:51:52Z")

</div>

On 7/6/06, Pratik [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And not have multiple boolean columns like in my people table like is  
> is\_goalkpeer, is\_forward, is\_defender.

Hi Pratik,  
I guess you have no way to use the STI in this scenario. I’d go with a  
Person model that contains all the data except the role and create a  
Role model/table.  
Then Role belongs\_to Person and Person has\_many Roles.  
Cheers.

---

<div class="post-metadata">

**Author:** ![pratik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4bbf92/32.png) [@pratik](https://www.ruby-forum.com/u/pratik)\
**Post date:** [July 7, 2006, 12:48am UTC](https://www.ruby-forum.com/t/single-table-inheritance/97414/5 "2006-07-07T00:48:09Z")

</div>

On Thursday, July 06, 2006, at 10:48 PM, Francesco Levorato wrote:

> > 
> 
> francesco levorato aka flevour  
> [http://www.flevour.net/](http://www.flevour.net/)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

For this type of thing, I use the acts\_as\_taggable plugin. Then you can  
tag each person with each role that they fulfill and you won’t clutter  
up your model with extraneous fields.

\_Kevin

---

<div class="post-metadata">

**Author:** ![pratik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4bbf92/32.png) [@pratik](https://www.ruby-forum.com/u/pratik)\
**Post date:** [July 6, 2006, 10:52pm UTC](https://www.ruby-forum.com/t/single-table-inheritance/97414/6 "2006-07-06T22:52:09Z")

</div>

On 7/6/06, Pratik [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And not have multiple boolean columns like in my people table like is  
> is\_goalkpeer, is\_forward, is\_defender.

Hi Pratik,  
I guess you have no way to use the STI in this scenario. I’d go with a  
Person model that contains all the data except the role and create a  
Role model/table.  
Then Role belongs\_to Person and Person has\_many Roles.  
Cheers.
