# Split a class by extending it, with a condition

**URL:** <https://www.ruby-forum.com/t/split-a-class-by-extending-it-with-a-condition/122202>\
**Category:** Rails\
**Created:** [December 5, 2007, 3:18pm UTC](https://www.ruby-forum.com/t/split-a-class-by-extending-it-with-a-condition/122202 "2007-12-05T15:18:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 5, 2007, 3:18pm UTC](https://www.ruby-forum.com/t/split-a-class-by-extending-it-with-a-condition/122202/1 "2007-12-05T15:18:14Z")

</div>

Let’s say i have a class with a lot of functionality, called Person. It  
happens that men and women are treated differently within my app and i’m  
sick of passing

:condition =\> ["gender = ? ", F]

all the time for cases dealing with women. Instead, i want to make two  
new classes, Man and Woman, which inherit all the functionality of  
Person but only some of the records, based on the above condition.

Can anyone tell me the syntax for this please? I know how to extend a  
class but not to limit it in this way.

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 5, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/split-a-class-by-extending-it-with-a-condition/122202/2 "2007-12-05T15:26:34Z")

</div>

On 5 Dec 2007, at 14:18, Max W. wrote:

> two  
> new classes, Man and Woman, which inherit all the functionality of  
> Person but only some of the records, based on the above condition.
> 
> Can anyone tell me the syntax for this please? I know how to extend a  
> class but not to limit it in this way.

Sounds like you want single table inheritance:  
Keep your existing Person class, with all the common code, and add a  
string column called ‘type’ to the table  
Now create classes Man and Woman that inherit from Person.

Now whenever you do somethine like Man.find :all, or has\_many :women  
etc… it will be scoped to Man or Woman as appropriate. You can still  
go Person.find :all to get everyone.

If you already have existing data, you’ll need to populate the newly  
created type column with the name of the appropriate subclass of Person.

Fred

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 5, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/split-a-class-by-extending-it-with-a-condition/122202/3 "2007-12-05T15:32:11Z")

</div>

Frederick C. wrote:

> On 5 Dec 2007, at 14:18, Max W. wrote:
> 
> > two  
> > new classes, Man and Woman, which inherit all the functionality of  
> > Person but only some of the records, based on the above condition.
> > 
> > Can anyone tell me the syntax for this please? I know how to extend a  
> > class but not to limit it in this way.
> 
> Sounds like you want single table inheritance:  
> Keep your existing Person class, with all the common code, and add a  
> string column called ‘type’ to the table  
> Now create classes Man and Woman that inherit from Person.
> 
> Now whenever you do somethine like Man.find :all, or has\_many :women  
> etc… it will be scoped to Man or Woman as appropriate. You can still  
> go Person.find :all to get everyone.
> 
> If you already have existing data, you’ll need to populate the newly  
> created type column with the name of the appropriate subclass of Person.
> 
> Fred

Ah, “single table inheritance” - i’d wondered before what that was, now  
i know the answer 🙂

Perfect, thanks!
