Is there a way to dynamically add new tables?

I don’t know the correct terminology to describe what I’m trying to do,
so I’ll just try to explain it the best I can.

I’m the guinea pig at work to start using Ruby and RoR. My project is
to make a web app that is basically just a simple drop down menu that
lists the names of all the tables in a particular MySQL database, then
when the user clicks on one of the table names, then hits “submit” it
will show them a nice neat html table of all the data in that table. I
have already made the list.rhtml file that shows all the data in a
table. What I need now is to start on the drop down menu. I could easily
make the drop down menu and hard code the names of all the tables into
it, but the kicker is that we need it to be dynamically updatable.
Meaning that if someone comes along and adds another table to the
database, or removes one, the drop down menu is automatically updated to
reflect this change and of course if you click on it and hit submit, it
will show you all the data.

So, forgive my ignorance, I’m new when it comes to Ruby and RoR, but is
there a way to, for lack of a better term, “sync” RoR with a database so
that it can automatically be updated when tables are added or removed,
or can you only “sync” it with the individual tables. Sorry for the bad
terminology, I hope you can understand what I am trying to do and what
my question is. In other words, will I have to run the
scaffold/controller/model scripts every time a new table is added, and
hard code the name of the new table into the drop down list, or is there
a way for ruby to automatically update itself anytime a new table is
added to the database?

Thanks in advance.

This isn’t exactly the “rails” way, but there is nothing stopping you
from selecting the list of tables from the RDBMS system tables.

I use DB2, Oracle, and Firebird, so I don’t know or have available the
specific MySQL for doing this.

Typically, it would be along the lines of “select TBNAME from
SYSTEM.SYSTABLES”.

I hope this helps.

jchahn wrote:

I don’t know the correct terminology to describe what I’m trying to do,
so I’ll just try to explain it the best I can.

So, forgive my ignorance, I’m new when it comes to Ruby and RoR, but is
there a way to, for lack of a better term, “sync” RoR with a database so
that it can automatically be updated when tables are added or removed,
or can you only “sync” it with the individual tables. Sorry for the bad
terminology, I hope you can understand what I am trying to do and what
my question is. In other words, will I have to run the
scaffold/controller/model scripts every time a new table is added, and
hard code the name of the new table into the drop down list, or is there
a way for ruby to automatically update itself anytime a new table is
added to the database?

Here’s what I’d do (and it’s not particularly elegant, but…):

  • ‘SHOW TABLES’ to get the list of available tables
  • Convert the table names into ruby constants (via Inflector::classify)
  • Check to see which (if any) of the constants haven’t been declared as
    classes
  • For each that hasn’t, module_eval the code “class #{class_name} <
    ActiveRecord::Base; end”
  • Use the complete list of classes to build the option list.

Do that in a before_filter, and the full set of classes will be
available to every controller action.