PDA

View Full Version : Dynamically Updated Database


jchahn
April 26th, 2006, 07:01 PM
At work everything we do is based on Java and Javascript. I am the guinea pig that they decided should try to make some apps using RoR, should Ruby and/or RoR turn out to be a beneficial tool for us, we will make more and more things with it. Like I said, I am the guinea pig.

Anyway, 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 database, then when the user clicks on one of the tables, 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 nice neat 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.

Thanks in advance.

rob
April 26th, 2006, 07:37 PM
In MySQL, you can do:

SHOW TABLES;

and get a list of the tables. I haven't tried this, but I bet making a manual call to the db with that SQL might just do the trick.

steve_d555
April 26th, 2006, 11:25 PM
Here (http://www.sitharus.com/articles/2006/04/07/dynamic-schemas-in-ruby-on-rails) is an example of a guy who did dynamic tables using PostgreSQL.

rob
April 26th, 2006, 11:37 PM
That's a pretty good example.

But I'm thinking that you just want to access already existing tables, and provide a drop-down of them, right? If so, I think a call to SHOW TABLES; with regular SQL and have the results be the option_for_select in the select tag, you should be good to go.

Of course, if you need to be able to create tables and delete them as well, I guess making an abstracted model like the link Steve posted is definately necessary.

And if you do just want to get a listing of the tables, it might not be a bad idea to add a class method to ActiveRecord::Base to do just that.