Forum: Ruby on Rails ActiveRecord: Dynamically-named tables sharing a common mode

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
51e61f099c7b1ac0588ecda69193f75c?d=identicon&s=25 Jim Stewart (Guest)
on 2007-06-06 21:51
(Received via mailing list)
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.

Ab0b702e7fcc79bb7d2c6c67f64e1b4d?d=identicon&s=25 tmac (Guest)
on 2007-06-06 22:11
(Received via mailing list)
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!
588ab1c0a5610a7e160a3b101abb91e6?d=identicon&s=25 MichaelLatta (Guest)
on 2007-06-06 22:13
(Received via mailing list)
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.

51e61f099c7b1ac0588ecda69193f75c?d=identicon&s=25 Jim Stewart (Guest)
on 2007-06-08 02:03
(Received via mailing list)
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.

This topic is locked and can not be replied to.