# Nuby: do models have to inherit directly from ActiveRecord?

**URL:** <https://www.ruby-forum.com/t/nuby-do-models-have-to-inherit-directly-from-activerecord/52445>\
**Category:** Rails\
**Created:** [January 14, 2006, 7:45am UTC](https://www.ruby-forum.com/t/nuby-do-models-have-to-inherit-directly-from-activerecord/52445 "2006-01-14T07:45:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_McGrath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@John\_McGrath](https://www.ruby-forum.com/u/John_McGrath)\
**Post date:** [January 14, 2006, 7:45am UTC](https://www.ruby-forum.com/t/nuby-do-models-have-to-inherit-directly-from-activerecord/52445/1 "2006-01-14T07:45:45Z")

</div>

Hello,

I have a few models – book, cd, dvd – for which I’d like to have an  
abstract base superclass to hold some common stuff. That abstract class,  
I  
was thinking, would inherit from ActiveRecord.

Didn’t work, though, and looking around, I found this:  
[http://wiki.rubyonrails.com/rails/pages/HowtoMakeAbstractModel](http://wiki.rubyonrails.com/rails/pages/HowtoMakeAbstractModel)  
[Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowtoMakeAbstractModel). Which  
looks  
pretty hackish to me. Is there any best way or recommendations on how to  
achieve this kind of behavior? I know I could modify ActiveRecord  
directly,  
but that seems brittle – to run my app on later versions of rails, I’d  
have  
to remember to re-modify ActiveRecord.

What’s really brittle, though, is my brain. This is my first RoR app,  
and  
parts of rails (convention over config, built-in ORM and testing) I  
immediately loved. But some stuff seems like it would be, well, [puts on  
fireproof suit] easier in Java. I’m still sort of waiting for it all to  
come  
together in my mind.

Anyways, apologies for being a dunce. Any guidance on how to best handle  
the  
abstract base class question would be much appreciated.

John

> **[John McGrath: solutions architect focused on startups, sustainability, media.](https://www.fryolator.com/)**
>
> I work primarily with early stage startups, with an emphasis on companies involved in sustainability. I've mentored and presented at TechStars Sustainability and Greentown Labs, and created the Code Green series of AWS sustainability workshops.

---

<div class="post-metadata">

**Author:** ![John\_McGrath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@John\_McGrath](https://www.ruby-forum.com/u/John_McGrath)\
**Post date:** [January 14, 2006, 8:57am UTC](https://www.ruby-forum.com/t/nuby-do-models-have-to-inherit-directly-from-activerecord/52445/2 "2006-01-14T08:57:42Z")

</div>

You should take a look at Single Table Inheritance (STI):  
[http://wiki.rubyonrails.com/rails/pages/SingleTableInheritance](http://wiki.rubyonrails.com/rails/pages/SingleTableInheritance)

/ CJ

---

<div class="post-metadata">

**Author:** ![John\_McGrath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@John\_McGrath](https://www.ruby-forum.com/u/John_McGrath)\
**Post date:** [January 14, 2006, 9:12am UTC](https://www.ruby-forum.com/t/nuby-do-models-have-to-inherit-directly-from-activerecord/52445/3 "2006-01-14T09:12:50Z")

</div>

Have you tried using Ruby module as mixin?

Chris

---

<div class="post-metadata">

**Author:** ![John\_McGrath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@John\_McGrath](https://www.ruby-forum.com/u/John_McGrath)\
**Post date:** [January 14, 2006, 5:38pm UTC](https://www.ruby-forum.com/t/nuby-do-models-have-to-inherit-directly-from-activerecord/52445/4 "2006-01-14T17:38:04Z")

</div>

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

> Hello,
> 
> I have a few models – book, cd, dvd – for which I’d like to have an  
> abstract base superclass to hold some common stuff. That abstract class, I  
> was thinking, would inherit from ActiveRecord.

I think this applies:  
[http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc](http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc)

You can’t do:  
class Media \< AR; end  
class Book \< Media; end  
class CD \< Media; end;

?

---

<div class="post-metadata">

**Author:** ![John\_McGrath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@John\_McGrath](https://www.ruby-forum.com/u/John_McGrath)\
**Post date:** [January 14, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/nuby-do-models-have-to-inherit-directly-from-activerecord/52445/5 "2006-01-14T18:30:26Z")

</div>

Joe Van D. wrote:

> On 1/13/06, John McGrath [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hello,
> > 
> > I have a few models – book, cd, dvd – for which I’d like to have an  
> > abstract base superclass to hold some common stuff. That abstract class, I  
> > was thinking, would inherit from ActiveRecord.
> 
> I think this applies:  
> [http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc](http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc)
> 
> You can’t do:  
> class Media \< AR; end  
> class Book \< Media; end  
> class CD \< Media; end;
> 
> ?

But you CAN do:

module Media  
#…  
end

class Book \< AR  
include Media  
end

class CD \< AR  
include Media  
end

---

<div class="post-metadata">

**Author:** ![John\_McGrath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@John\_McGrath](https://www.ruby-forum.com/u/John_McGrath)\
**Post date:** [January 14, 2006, 12:44pm UTC](https://www.ruby-forum.com/t/nuby-do-models-have-to-inherit-directly-from-activerecord/52445/6 "2006-01-14T12:44:01Z")

</div>

John McGrath wrote:

> I have a few models – book, cd, dvd – for which I’d like to have an  
> abstract base superclass to hold some common stuff. That abstract  
> class, I was thinking, would inherit from ActiveRecord.
> 
> Didn’t work, though, and looking around, I found this:  
> [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowtoMakeAbstractModel). Which  
> looks pretty hackish to me. Is there any best way or recommendations on  
> how to achieve this kind of behavior? I know I could modify ActiveRecord  
> directly, but that seems brittle – to run my app on later versions of  
> rails, I’d have to remember to re-modify ActiveRecord.

What I did once was to put the common stuff in a module, called say  
Media  
in your case, and put ‘include Media’ at the top of each model class.

Instance methods will include directly, but for class methods that you  
want to call without using the “Media.” prefix you have to define a  
self.included(model) method in the module that defines the class methods  
in a model.class\_eval block.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![John\_McGrath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@John\_McGrath](https://www.ruby-forum.com/u/John_McGrath)\
**Post date:** [January 21, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/nuby-do-models-have-to-inherit-directly-from-activerecord/52445/7 "2006-01-21T15:40:03Z")

</div>

On 1/14/06, Ben M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > class CD \< AR  
> > include Media  
> > end
> 
> Just to clarify here: Joe was NOT saying that you CAN’T have a base class… note the “?”  
> after the “You can’t do:” bit. He was saying “why can’t you just do…?”, right Joe?

Yes, that’s right.

> It seems that either approach will work, though now I’m even more confused. I thought the  
> STI requirement in rails precluded base model classes (that don’t map to tables). From the  
> Dave T. blog entry it looks like it’s okay as long as you don’t try to call db methods  
> on the base class.
> 
> right?

Yeah, I think it’s fine as long as the base class is abstract (i.e.  
you don’t try to instantiate it or try to call any db methods with  
it).

---

<div class="post-metadata">

**Author:** ![John\_McGrath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@John\_McGrath](https://www.ruby-forum.com/u/John_McGrath)\
**Post date:** [January 14, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/nuby-do-models-have-to-inherit-directly-from-activerecord/52445/8 "2006-01-14T20:25:11Z")

</div>

Jules J. wrote:

> But you CAN do:  
> include Media  
> end

Just to clarify here: Joe was NOT saying that you CAN’T have a base  
class… note the “?”  
after the “You can’t do:” bit. He was saying “why can’t you just  
do…?”, right Joe?

It seems that either approach will work, though now I’m even more  
confused. I thought the  
STI requirement in rails precluded base model classes (that don’t map to  
tables). From the  
Dave T. blog entry it looks like it’s okay as long as you don’t try  
to call db methods  
on the base class.

right?

b
