# ActiveRecord: Getting table names

**URL:** <https://www.ruby-forum.com/t/activerecord-getting-table-names/62332>\
**Category:** Rails\
**Created:** [June 2, 2006, 8:13pm UTC](https://www.ruby-forum.com/t/activerecord-getting-table-names/62332 "2006-06-02T20:13:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahulbenegal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@rahulbenegal](https://www.ruby-forum.com/u/rahulbenegal)\
**Post date:** [June 2, 2006, 8:13pm UTC](https://www.ruby-forum.com/t/activerecord-getting-table-names/62332/1 "2006-06-02T20:13:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rahulbenegal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@rahulbenegal](https://www.ruby-forum.com/u/rahulbenegal)\
**Post date:** [June 2, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/activerecord-getting-table-names/62332/2 "2006-06-02T21:27:36Z")

</div>

Maybe you should take a look at ActiveRecord::ConnectionAdapters (MySql,  
Postgres, SQLServer…) on RoR api. If u hadnt till now.

---

<div class="post-metadata">

**Author:** ![rahulbenegal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@rahulbenegal](https://www.ruby-forum.com/u/rahulbenegal)\
**Post date:** [June 3, 2006, 3:27am UTC](https://www.ruby-forum.com/t/activerecord-getting-table-names/62332/3 "2006-06-03T03:27:25Z")

</div>

The .tables method returns an array of tables. The  
connection  
is accessible via your model classes, or instances - i.e.  
.connection, or .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:

---

<div class="post-metadata">

**Author:** ![rahulbenegal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@rahulbenegal](https://www.ruby-forum.com/u/rahulbenegal)\
**Post date:** [June 3, 2006, 6:18am UTC](https://www.ruby-forum.com/t/activerecord-getting-table-names/62332/4 "2006-06-03T06:18:35Z")

</div>

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/](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:

---

<div class="post-metadata">

**Author:** ![rahulbenegal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@rahulbenegal](https://www.ruby-forum.com/u/rahulbenegal)\
**Post date:** [June 3, 2006, 11:54am UTC](https://www.ruby-forum.com/t/activerecord-getting-table-names/62332/5 "2006-06-03T11:54:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rahulbenegal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@rahulbenegal](https://www.ruby-forum.com/u/rahulbenegal)\
**Post date:** [June 4, 2006, 6:32am UTC](https://www.ruby-forum.com/t/activerecord-getting-table-names/62332/6 "2006-06-04T06:32:47Z")

</div>

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
