# Using ActiveRecord in subclasses

**URL:** <https://www.ruby-forum.com/t/using-activerecord-in-subclasses/182335>\
**Category:** Ruby\
**Created:** [January 26, 2010, 6:27pm UTC](https://www.ruby-forum.com/t/using-activerecord-in-subclasses/182335 "2010-01-26T18:27:01Z")\
**Posts on this page:** 2\
**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:27pm UTC](https://www.ruby-forum.com/t/using-activerecord-in-subclasses/182335/1 "2010-01-26T18:27:01Z")

</div>

Hi, 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, 6:52pm UTC](https://www.ruby-forum.com/t/using-activerecord-in-subclasses/182335/2 "2010-01-26T18:52:40Z")

</div>

Reposted in the Ruby on Rails forum. Apologies.
