# ActiveRecord: Dynamically-named tables sharing a common mode

**URL:** <https://www.ruby-forum.com/t/activerecord-dynamically-named-tables-sharing-a-common-mode/106151>\
**Category:** Rails\
**Created:** [June 6, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/activerecord-dynamically-named-tables-sharing-a-common-mode/106151 "2007-06-06T21:51:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jim\_Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@Jim\_Stewart](https://www.ruby-forum.com/u/Jim_Stewart)\
**Post date:** [June 6, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/activerecord-dynamically-named-tables-sharing-a-common-mode/106151/1 "2007-06-06T21:51:38Z")

</div>

I’ve got a legacy database that I’m trying to model in ActiveRecord.  
The database consists of many pairs of tables (e.g. Foo\_Sources and  
Foo\_Procedures, Bar\_Sources and Bar\_Procedures). There will be  
multiple pairs of tables, serving different purposes but sharing a  
common schema (Foo\_Sources and Bar\_Sources will be identical except  
for the table name).

I’d like to model these in ActiveRecord, but I’m not sure how to  
approach the problem. The tables I access will be determined at  
runtime, and the database itself is dynamic (Baz\_Sources will appear  
in the future, and I don’t want to have to change the code to support  
it).

Anyone have any ideas? I’m a relative newcomer to Ruby/Rails, but I  
get the impression something like this could be handled since Ruby is  
such a dynamic language. Maybe some kind of ActiveRecord class  
factory?

Any suggestions would be appreciated! Thanks in advance.

Jim

---

<div class="post-metadata">

**Author:** ![Jim\_Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@Jim\_Stewart](https://www.ruby-forum.com/u/Jim_Stewart)\
**Post date:** [June 6, 2007, 10:13pm UTC](https://www.ruby-forum.com/t/activerecord-dynamically-named-tables-sharing-a-common-mode/106151/2 "2007-06-06T22:13:43Z")

</div>

There are many approaches to this. First let me point out that unlike  
almost all other languages, in Ruby you can create classes at run-time  
in the language.

So you can execute “foo \< ActiveRecord::Base” whenever you like for  
any table you know to exist. You can use a static table to get a  
connection to the database and execute any SQL you like to obtain the  
list of tables you care about. Since ActiveRecord looks at the  
database table for attribute data the fact that they share schema is  
coincidental but does not really affect the issue. Since ruby uses  
duck typing you do not need to tell the system they share a schema,  
you just happen to use the same methods to access their data.

In your Rails application you just need to add the proper bootstrap  
code to look for all the dynamic tables and build ActiveRecord  
subclasses for each table.

Michael

---

<div class="post-metadata">

**Author:** ![Jim\_Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@Jim\_Stewart](https://www.ruby-forum.com/u/Jim_Stewart)\
**Post date:** [June 6, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/activerecord-dynamically-named-tables-sharing-a-common-mode/106151/3 "2007-06-06T22:11:40Z")

</div>

Sounds like a case for STI (single table inheritance) to me.

For examlple:  
\*create a table named sources and it’s associated model with all your  
attributes plus the “type” attribute  
\*create your models e.g… FooSources, BarSources that inherit from  
Sources  
\*create a little script, or just do it manually from your db client,  
to move all the data to the new table

something like that anyway;-)

Now anytime you want to create another table you don’t have to, you  
just create a new model that inherits from Sources.

Hope that made some sense?

A bit more on STI in rails here:

> **[Rails Single Table Inheritance](http://juixe.com/techknow/index.php/2006/06/03/rails-single-table-inheritance/)**
>
> Ruby on Rails supports Single Table Inheritance. Single Table Inheritance allows you to have one single SQL table, such as an employees table, manage different type of employees like managers and d…

Good Luck!  
Tim

---

<div class="post-metadata">

**Author:** ![Jim\_Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@Jim\_Stewart](https://www.ruby-forum.com/u/Jim_Stewart)\
**Post date:** [June 8, 2007, 2:03am UTC](https://www.ruby-forum.com/t/activerecord-dynamically-named-tables-sharing-a-common-mode/106151/4 "2007-06-08T02:03:59Z")

</div>

That’s roughly the idea I had in mind when I mentioned an ActiveRecord  
class factory. I think it’ll work well.

The current temporary solution is to just call :set\_table\_name on the  
model class as needed. It works, but there are plenty of limitations  
to that approach.

Thanks for the tip.

Jim
