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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jim Stewart (Guest)
on 2007-06-06 23: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
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
tmac (Guest)
on 2007-06-07 00: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
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:
http://www.juixe.com/techknow/index.php/2006/06/03...

Good Luck!
Tim
MichaelLatta (Guest)
on 2007-06-07 00: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.

Michael
Jim Stewart (Guest)
on 2007-06-08 04: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.

Jim
This topic is locked and can not be replied to.