# How do I list all the tables in a database?

**URL:** <https://www.ruby-forum.com/t/how-do-i-list-all-the-tables-in-a-database/83353>\
**Category:** Rails\
**Created:** [January 25, 2007, 3:04pm UTC](https://www.ruby-forum.com/t/how-do-i-list-all-the-tables-in-a-database/83353 "2007-01-25T15:04:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 25, 2007, 3:04pm UTC](https://www.ruby-forum.com/t/how-do-i-list-all-the-tables-in-a-database/83353/1 "2007-01-25T15:04:30Z")

</div>

Hi,  
how do I list all the tables in a database?  
I’ve googled for a time but din’t found this information.  
Is there an object to the database?

tkns in advance.

---

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 25, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/how-do-i-list-all-the-tables-in-a-database/83353/2 "2007-01-25T15:12:04Z")

</div>

Hi P. Pantouffe,

> how do I list all the tables in a database?

with : ActiveRecord::Base.connection.tables

– Jean-François.

–  
À la renverse.

---

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 25, 2007, 5:03pm UTC](https://www.ruby-forum.com/t/how-do-i-list-all-the-tables-in-a-database/83353/3 "2007-01-25T17:03:08Z")

</div>

Thank u!  
I have another one :). How can i get a list with all the classes in the  
application after i wrote several models. ?

---

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 25, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/how-do-i-list-all-the-tables-in-a-database/83353/4 "2007-01-25T17:17:52Z")

</div>

P. Pantouffe :

> I have another one :). How can i get a list with all the classes in the  
> application after i wrote several models. ?

In production mode, for AR:B-based models :

ActiveRecord::Base.send(:subclasses)

but I’m not so sure…

– Jean-François.

–  
À la renverse.

---

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 25, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/how-do-i-list-all-the-tables-in-a-database/83353/5 "2007-01-25T17:26:01Z")

</div>

the otput for  
\<%= ActiveRecord::Base.send(:subclasses).to\_s %\> is  
CGI::Session::ActiveRecordStore::SessionMenuelement  
i am in development, should these be the cause?

---

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 25, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/how-do-i-list-all-the-tables-in-a-database/83353/6 "2007-01-25T17:31:39Z")

</div>

P. Pantouffe wrote:

> Thank u!  
> I have another one :). How can i get a list with all the classes in the  
> application after i wrote several models. ?

This code, derived from annotate plugin  
([http://svn.pragprog.com/Public/plugins/annotate\_models/](http://svn.pragprog.com/Public/plugins/annotate_models/)), processes all  
models contained in app/models/

def model\_files  
Dir.chdir(MODEL\_DIR) do  
models = Dir[“\*\*/\*.rb”]  
end  
end

def process\_model\_files  
model\_files.each do |m|  
class\_name = m.sub(/.rb$/,‘’).camelize  
klass = class\_name.split(‘::’).inject(Object){ |klass,part|  
klass.const\_get(part) } rescue nil  
if klass && klass \< ActiveRecord::Base && ! klass.abstract\_class?  
#your code  
end  
end  
end

regards  
Massimo  
[http://www.addsw.it](http://www.addsw.it)

---

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 25, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/how-do-i-list-all-the-tables-in-a-database/83353/7 "2007-01-25T19:22:34Z")

</div>

Not exactly what you are asking for, but might be useful:  
[http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/AnnotateModels.rdoc](http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/AnnotateModels.rdoc)

it annotates your models

On Jan 25, 8:03 am, “P. Pantouffe” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
