# Subclassing a model

**URL:** <https://www.ruby-forum.com/t/subclassing-a-model/193556>\
**Category:** Rails\
**Created:** [August 19, 2010, 5:05pm UTC](https://www.ruby-forum.com/t/subclassing-a-model/193556 "2010-08-19T17:05:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 19, 2010, 5:05pm UTC](https://www.ruby-forum.com/t/subclassing-a-model/193556/1 "2010-08-19T17:05:13Z")

</div>

Is it possible to define a subclass for a model, that defines additional  
data columns, which are stored in a separate table?

Is there any description, how to do this?

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 19, 2010, 5:24pm UTC](https://www.ruby-forum.com/t/subclassing-a-model/193556/2 "2010-08-19T17:24:44Z")

</div>

On Thu, Aug 19, 2010 at 8:05 AM, Fritz T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Is it possible to define a subclass for a model, that defines additional  
> data columns, which are stored in a separate table?

What you are describing is “Multiple Table Inheritance”. I don’t  
think stock rails handles this. Rails supports “Single Table  
Inheritance”[1] in which you’ll store the union of all column types in  
the base table.

[1] [ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html)

> Is there any description, how to do this?

For STI make a table for the base class and subclass arbitrarily.

–  
med vänlig hälsning  
David JH

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 19, 2010, 5:25pm UTC](https://www.ruby-forum.com/t/subclassing-a-model/193556/3 "2010-08-19T17:25:37Z")

</div>

David J. Hamilton wrote:

> On Thu, Aug 19, 2010 at 8:05 AM, Fritz T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Is it possible to define a subclass for a model, that defines additional  
> > data columns, which are stored in a separate table?
> 
> What you are describing is “Multiple Table Inheritance”. I don’t  
> think stock rails handles this. Rails supports “Single Table  
> Inheritance”[1] in which you’ll store the union of all column types in  
> the base table.

But you could use has\_one.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 19, 2010, 5:29pm UTC](https://www.ruby-forum.com/t/subclassing-a-model/193556/4 "2010-08-19T17:29:25Z")

</div>

On 19 August 2010 16:05, Fritz T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is it possible to define a subclass for a model, that defines additional  
> data columns, which are stored in a separate table?
> 
> Is there any description, how to do this?

You can certainly subclass a model.

Then the subclass could have a :has\_one relationship to another model  
with the extra columns which the superclass doesn’t have.

If you want to make it transparent, you can delegate the calls for the  
extra tables in the associated model, so instead of:  
foo.bar.extra\_value  
you can call:  
foo.extra\_value

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 19, 2010, 6:36pm UTC](https://www.ruby-forum.com/t/subclassing-a-model/193556/5 "2010-08-19T18:36:54Z")

</div>

Oh thanks. It seems, I not see wood for trees…
