How do i get (discover) all the table names within a database using ROR/ActiveRecord ? I wish to build a webapp that connects to a database on the fly, discovers tables and column names etc and then can show data or build a report based on user selection. The ActiveRecord API doesnt seem to give any method for getting table names.
on 2006-06-02 22:13
on 2006-06-02 23:27
Maybe you should take a look at ActiveRecord::ConnectionAdapters (MySql, Postgres, SQLServer....) on RoR api. If u hadnt till now.
on 2006-06-03 05:27
The <connection>.tables method returns an array of tables. The connection is accessible via your model classes, or instances - i.e. <ModelClass>.connection, or <any model instance>.connection. In the API docs, you will find the "tables" method documented for DB2 and SQLserver, but the method is annotated in the MySql and Postgres adapters with #:nodoc:
on 2006-06-03 08:18
Thanks a lot. Does this mean that in order to write a generic database browser like application I would have to hardcode database specific Select statements to retrieve tables, and other information. Yes, as far as mysql and others, the methods don't even exist (I am looking at:http://api.rubyonrails.org/). Where did you see the %:nodoc? Even OG and DBI dont seem to have anything generic. rahul benegal Vince Nibler wrote: > In the API docs, you will find the "tables" method documented for DB2 > and > SQLserver, but the method is annotated in the MySql and Postgres > adapters > with #:nodoc:
on 2006-06-03 13:54
No. The method is there for MySQL and Postgres, just not documented because of the annotation. Take a look at the mysql_adapter.rb code in your ruby gems activerecord directory. On my windows box, it's here: C:\ruby\lib\ruby\gems\1.8\gems\activerecord- 1.14.2\lib\active_record\connection_adapters (let me know if you need help finding it on a mac) For the four adapters mentioned - mysql, postgres, db2 and sqlserver - the tables method is implemented. You'll have to take a look for the other adapters (just checked, it's implemented for Oracle too). Probably easy enough to implement for any others, given all of those examples (they're all pretty short, too) Keep us posted. It would be great to have a rails plugin to admin databases in liu of phpmyadmin or phppgadmin. Vince
on 2006-06-04 08:32
Actually phpmyadmin was an example. I had once written a report generator in Java that could generate complex reports such as multi-level cross-tabs etc. However, the problem was that each time a report was generated, the server had to be rebooted since new classes were also created. Moving to Ruby and ROR, i was looking for what i could do that would be interesting, and i thought i could redo that in ruby. With ruby's non-strict typing and other features, i thought i would be easier. Plus, it didnt look like rails has a report generator (at least when i played with rails last year). However, a rails/ruby application (plugin???) like phpmyadmin would be interesting also. Rahul Benegal Vince Nibler wrote: > > Keep us posted. It would be great to have a rails plugin to admin > databases > in liu of phpmyadmin or phppgadmin. > > Vince