# Subclassing ActiveRecord::Base

**URL:** <https://www.ruby-forum.com/t/subclassing-activerecord-base/182338>\
**Category:** Rails\
**Created:** [January 26, 2010, 6:51pm UTC](https://www.ruby-forum.com/t/subclassing-activerecord-base/182338 "2010-01-26T18:51:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul-au](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@paul-au](https://www.ruby-forum.com/u/paul-au)\
**Post date:** [January 26, 2010, 6:51pm UTC](https://www.ruby-forum.com/t/subclassing-activerecord-base/182338/1 "2010-01-26T18:51:29Z")

</div>

First of all, sorry for the crossposting, but I put this into the Ruby  
Forum first of all, but was pointed to this as a more appropriate  
location.

I’m using ActiveRecord and ActiveSupport in a non-rails environment  
to connect to multiple databases, and I’ve found the following (single  
database) to cause me an error. Note that params is my database settings  
and omitted for obvious reasons. pages, components, and books are all  
tables in the same db.

require ‘active\_record’  
module TestDB  
class Inherited \< ActiveRecord::Base  
self.logger  
self.default\_timezone :utc  
self.establish\_connection(params)  
end  
class Page \< Inherited  
puts self.name  
puts self.table\_name  
end  
class Book \< ActiveRecord::Base  
puts self.name  
puts self.table\_name  
end  
end  
class Component \< ActiveRecord::Base  
puts self.name  
puts self.table\_name  
end

outputs:

TestDB::Page \<= expected  
inheriteds \<= Hmm, table name for the parent?  
TestDB::Book \<= also expected  
books \<= Seems to be using the Inherited connection  
Component \<= Yep, no module name  
components \<= also using the Inherited connection

Is this expected behaviour from an inherited class. Is there anyway to  
scope the ActiveRecord connection without resorting to plugins or other  
gems?

Mac

---

<div class="post-metadata">

**Author:** ![paul-au](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@paul-au](https://www.ruby-forum.com/u/paul-au)\
**Post date:** [January 26, 2010, 7:16pm UTC](https://www.ruby-forum.com/t/subclassing-activerecord-base/182338/2 "2010-01-26T19:16:43Z")

</div>

On Jan 26, 5:51 pm, Paul M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘active\_record’  
> module TestDB  
> class Inherited \< ActiveRecord::Base  
> self.logger  
> self.default\_timezone :utc  
> self.establish\_connection(params)

You should set self.abstract\_class = true on this class or  
activerecord might think you’re trying single table inheritance.

Fred

---

<div class="post-metadata">

**Author:** ![paul-au](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@paul-au](https://www.ruby-forum.com/u/paul-au)\
**Post date:** [January 26, 2010, 9:12pm UTC](https://www.ruby-forum.com/t/subclassing-activerecord-base/182338/3 "2010-01-26T21:12:21Z")

</div>

Frederick C. wrote:

> On Jan 26, 5:51ï¿½pm, Paul M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > require ‘active\_record’  
> > module TestDB  
> > ï¿½ class Inherited \< ActiveRecord::Base  
> > ï¿½ ï¿½ self.logger  
> > ï¿½ ï¿½ self.default\_timezone :utc  
> > ï¿½ ï¿½ self.establish\_connection(params)
> 
> You should set self.abstract\_class = true on this class or  
> activerecord might think you’re trying single table inheritance.
> 
> Fred

I’ve used inheritance with many of my models. To expand upon what Fred  
is saying here, you really need to do two things. I’ll show you an  
example use and then you can configure your own class to use it:

One of my models is an inheritance template:

class InheritanceTemplate \< ActiveRecord::Base  
self.abstract\_class = true  
end

Other models inherit from this template by doing the following:

class MyPage \< InheritanceTemplate  
set\_table\_name “my\_pages” #pluralized table name  
end

etc.

Notice in the classes that are using the inheritance template you should  
also set the table name so that there’s no confusion going on in the  
templates.

---

<div class="post-metadata">

**Author:** ![paul-au](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@paul-au](https://www.ruby-forum.com/u/paul-au)\
**Post date:** [January 26, 2010, 10:24pm UTC](https://www.ruby-forum.com/t/subclassing-activerecord-base/182338/4 "2010-01-26T22:24:40Z")

</div>

On Jan 26, 8:12 pm, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Notice in the classes that are using the inheritance template you should  
> also set the table name so that there’s no confusion going on in the  
> templates.

I’ve never needed to do that.

Fred

---

<div class="post-metadata">

**Author:** ![paul-au](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@paul-au](https://www.ruby-forum.com/u/paul-au)\
**Post date:** [January 31, 2010, 6:54pm UTC](https://www.ruby-forum.com/t/subclassing-activerecord-base/182338/5 "2010-01-31T18:54:05Z")

</div>

Alpha B. wrote:

> Frederick C. wrote:
> 
> > On Jan 26, 5:51ï¿½pm, Paul M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> > class MyPage \< InheritanceTemplate  
> > set\_table\_name “my\_pages” #pluralized table name  
> > end
> 
> etc.
> 
> Notice in the classes that are using the inheritance template you should  
> also set the table name so that there’s no confusion going on in the  
> templates.

I was doing that in the models that I had inherited, but if there isn’t  
a way to do it without setting the table name then I don’t see the  
benefit.

Paul

---

<div class="post-metadata">

**Author:** ![paul-au](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@paul-au](https://www.ruby-forum.com/u/paul-au)\
**Post date:** [January 26, 2010, 10:40pm UTC](https://www.ruby-forum.com/t/subclassing-activerecord-base/182338/6 "2010-01-26T22:40:01Z")

</div>

Frederick C. wrote:

> On Jan 26, 8:12ï¿½pm, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Notice in the classes that are using the inheritance template you should  
> > also set the table name so that there’s no confusion going on in the  
> > templates.
> 
> I’ve never needed to do that.
> 
> Fred

It depends on what he’s going to do with the code. In my case, I have  
several archive tables that apply to some of my inheritance models. I  
only placed it in there as an example. 🙂

---

<div class="post-metadata">

**Author:** ![paul-au](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@paul-au](https://www.ruby-forum.com/u/paul-au)\
**Post date:** [January 31, 2010, 7:39pm UTC](https://www.ruby-forum.com/t/subclassing-activerecord-base/182338/7 "2010-01-31T19:39:44Z")

</div>

Paul M. wrote:

> Alpha B. wrote:
> 
> > Frederick C. wrote:
> > 
> > > On Jan 26, 5:51ï¿½pm, Paul M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> > > class MyPage \< InheritanceTemplate  
> > > set\_table\_name “my\_pages” #pluralized table name  
> > > end
> > 
> > etc.
> > 
> > Notice in the classes that are using the inheritance template you should  
> > also set the table name so that there’s no confusion going on in the  
> > templates.
> 
> I was doing that in the models that I had inherited, but if there isn’t  
> a way to do it without setting the table name then I don’t see the  
> benefit.

As Fred said, setting the table name is probably unnecessary.

> Paul

## Best,

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