# ActiveRecord inheritance

**URL:** <https://www.ruby-forum.com/t/activerecord-inheritance/98221>\
**Category:** Rails\
**Created:** [July 13, 2006, 8:36am UTC](https://www.ruby-forum.com/t/activerecord-inheritance/98221 "2006-07-13T08:36:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iseng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8baadc/32.png) [@iseng](https://www.ruby-forum.com/u/iseng)\
**Post date:** [July 13, 2006, 8:36am UTC](https://www.ruby-forum.com/t/activerecord-inheritance/98221/1 "2006-07-13T08:36:55Z")

</div>

How to create a super class based on ActiveRecord::Base?

For example:

class ParentRecord \< ActiveRecord::Base  
end

class ChildRecord \< ParentRecord  
end

Rails always asks for ‘parent\_records’ table. Which has absolutely no  
need to exist at all. It’s intentionally made as a super class to be  
inherited.

Thanks.

---

<div class="post-metadata">

**Author:** ![iseng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8baadc/32.png) [@iseng](https://www.ruby-forum.com/u/iseng)\
**Post date:** [July 13, 2006, 8:51am UTC](https://www.ruby-forum.com/t/activerecord-inheritance/98221/2 "2006-07-13T08:51:42Z")

</div>

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

> Rails always asks for ‘parent\_records’ table. Which has absolutely no  
> need to exist at all. It’s intentionally made as a super class to be  
> inherited.
> 
> Thanks.

By doing this rails assumes you want to use single table inheritance.  
To do  
this you have a parent\_records table with a type (string) field.

There is a fair bit of information regarding STI kicking around  
especially  
on the wiki

–

---

<div class="post-metadata">

**Author:** ![iseng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8baadc/32.png) [@iseng](https://www.ruby-forum.com/u/iseng)\
**Post date:** [July 13, 2006, 10:43am UTC](https://www.ruby-forum.com/t/activerecord-inheritance/98221/3 "2006-07-13T10:43:57Z")

</div>

Daniel ----- wrote:

> By doing this rails assumes you want to use single table inheritance.  
> To do  
> this you have a parent\_records table with a type (string) field.
> 
> There is a fair bit of information regarding STI kicking around  
> especially  
> on the wiki
> 
> –

Hi Daniel thanks for the reply.

The thing is, I don’t want to use Single Table Inheritance. I have  
several record objects based on TextRecord (which incorporates all text  
formatting functions).

ActiveRecord::Base \> TextRecord  
TextRecord \> ForumPost  
TextRecord \> Article  
TextRecord \> PrivateMessage  
etc

I want separate tables for each and everyone: forum\_posts, articles,  
private\_messages, etc.

---

<div class="post-metadata">

**Author:** ![iseng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8baadc/32.png) [@iseng](https://www.ruby-forum.com/u/iseng)\
**Post date:** [July 13, 2006, 11:04am UTC](https://www.ruby-forum.com/t/activerecord-inheritance/98221/4 "2006-07-13T11:04:32Z")

</div>

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

> > –  
> > TextRecord \> PrivateMessage  
> > etc
> 
> I want separate tables for each and everyone: forum\_posts, articles,  
> private\_messages, etc.

In this case, you would be best to put the common methods into a module  
and  
then include this module in the classes

eg  
module MyModule  
def my\_method  
do\_something  
end  
end

Put this into the lib directory of your app structure and put the  
include  
statement into your classes  
class PrivateMessage \< ActiveRecord::Base  
include MyModule  
…  
end  
etc

This will provide the methods as instance methods on your classes.

–

---

<div class="post-metadata">

**Author:** ![iseng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8baadc/32.png) [@iseng](https://www.ruby-forum.com/u/iseng)\
**Post date:** [July 13, 2006, 10:54am UTC](https://www.ruby-forum.com/t/activerecord-inheritance/98221/5 "2006-07-13T10:54:50Z")

</div>

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

> etc  
> class TextRecord \< ActiveRecord::Base  
> self.abstract\_class = true  
> end

class ForumPost \< TextRecord  
end

etc…

Tom

---

<div class="post-metadata">

**Author:** ![iseng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8baadc/32.png) [@iseng](https://www.ruby-forum.com/u/iseng)\
**Post date:** [July 13, 2006, 11:04am UTC](https://www.ruby-forum.com/t/activerecord-inheritance/98221/6 "2006-07-13T11:04:39Z")

</div>

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

> > > There is a fair bit of information regarding STI kicking around
> 
> In this case, you would be best to put the common methods into a module  
> statement into your classes  
> class PrivateMessage \< ActiveRecord::Base  
> include MyModule  
> …  
> end  
> etc
> 
> This will provide the methods as instance methods on your classes.

Or do what Tom suggests 😉 Thanx Tom. I missed that one

–
