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. -- Posted via http://www.ruby-forum.com/.
Maybe you should take a look at ActiveRecord::ConnectionAdapters (MySql, Postgres, SQLServer....) on RoR api. If u hadnt till now. -- Cheers, ioana k&a http://boulangerie.wordpress.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060602/fd6cbdd1/attachment.html
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 6/2/06, Ioana Kanda <kaio4000@gmail.com> wrote:> > Maybe you should take a look at ActiveRecord::ConnectionAdapters (MySql, > Postgres, SQLServer....) on RoR api. If u hadnt till now. > -- > Cheers, > ioana k&a > http://boulangerie.wordpress.com/ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Vince -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060603/652cd7b2/attachment.html
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:-- Posted via http://www.ruby-forum.com/.
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 6/2/06, rahul benegal <rahul_kumar@yahoo.com> wrote:> > 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: > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060603/b4512099/attachment-0001.html
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-- Posted via http://www.ruby-forum.com/.