# Using ActiveRecord to determine database structure

**URL:** <https://rubytalk.org/t/using-activerecord-to-determine-database-structure/32231>\
**Category:** ruby-talk\
**Created:** [24 October 2006 09:10 UTC](https://rubytalk.org/t/using-activerecord-to-determine-database-structure/32231 "2006-10-24T09:10:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karla](https://avatars.discourse-cdn.com/v4/letter/k/87869e/32.png) [@Karla](https://rubytalk.org/u/Karla)\
**Post date:** [24 October 2006 09:10 UTC](https://rubytalk.org/t/using-activerecord-to-determine-database-structure/32231/1 "2006-10-24T09:10:15Z")

</div>

Hi Folks,

for a project i have to write a program that determines automatically  
the stucture of a given database. This means, i have to find out which  
tables a database has, which attributes those tables have, which type  
those attributes have and also the table constraints.

For this purpose i wanted to use ActiveRecord because as far as i know  
it is capable of doing all this things.  
I did the "Rolling with Ruby on Rails" tutorial so i know that the  
scaffolding mechanism can be used to determine a table's structure.  
When looking into the ActiveRecord sources i found a method named  
"structure\_dump" in  
connection\_adapters/abstract/schema\_statements.rb, i guess i can use  
that to find out of which tables a database consists.

Since i'm very new to Ruby i have a lot of problems to implement those  
things. What i already achieved is to connect to a database with  
ActiveRecord like this

ActiveRecord::Base.establish\_connection(  
&nbsp;&nbsp;:host =\> "localhost" ,  
&nbsp;&nbsp;:adapter =\> "mysql",  
&nbsp;&nbsp;:database =\> "test" ,  
&nbsp;&nbsp;:username =\> "user" ,  
&nbsp;&nbsp;:password =\> "password" ,  
&nbsp;&nbsp;:socket =\> "/var/run/mysqld/mysqld.sock"  
)

My question is if somebody kindly provide me with some sort of small  
example of how to determine a Database's description with ActiveRecord.  
This would be very nice.

With kind regards,  
Karla
