ActiveRecord: Dynamically-named tables sharing a common mode

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

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

Any suggestions would be appreciated! Thanks in advance.


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.


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
*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:

Good Luck!

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.