# How to query the schema of a ActiveRecord class?

**URL:** <https://www.ruby-forum.com/t/how-to-query-the-schema-of-a-activerecord-class/193185>\
**Category:** Rails\
**Created:** [August 12, 2010, 6:48pm UTC](https://www.ruby-forum.com/t/how-to-query-the-schema-of-a-activerecord-class/193185 "2010-08-12T18:48:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 12, 2010, 6:48pm UTC](https://www.ruby-forum.com/t/how-to-query-the-schema-of-a-activerecord-class/193185/1 "2010-08-12T18:48:14Z")

</div>

When I enter the name of a ActiveRecord class to irb, the schema of the  
record is displayed:

Art  
Art(id: integer, name: string, created\_at: datetime, updated\_at:  
datetime)

How to query that vir ruby code?

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 12, 2010, 7:04pm UTC](https://www.ruby-forum.com/t/how-to-query-the-schema-of-a-activerecord-class/193185/2 "2010-08-12T19:04:57Z")

</div>

On Aug 12, 12:48 pm, Fritz T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When I enter the name of a ActiveRecord class to irb, the schema of the  
> record is displayed:
> 
> Art  
> Art(id: integer, name: string, created\_at: datetime, updated\_at:  
> datetime)
> 
> How to query that vir ruby code?

Take a peak at Art.columns

Fred

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 12, 2010, 7:20pm UTC](https://www.ruby-forum.com/t/how-to-query-the-schema-of-a-activerecord-class/193185/3 "2010-08-12T19:20:10Z")

</div>

On Thu, Aug 12, 2010 at 9:48 AM, Fritz T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> When I enter the name of a ActiveRecord class to irb, the schema of the  
> record is displayed:

> How to query that vir ruby code?

e.g.

> > Author  
> > =\> Authorid: integer, first\_name: string, last\_name: string, dob:  
> > date, created\_at: datetime, updated\_at: datetime, genre: string  
> > Author.inspect  
> > =\> “Author(id: integer, first\_name: string, last\_name: string, dob:  
> > date, created\_at: datetime, updated\_at: datetime, genre: string)”

## HTH,

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 12, 2010, 7:40pm UTC](https://www.ruby-forum.com/t/how-to-query-the-schema-of-a-activerecord-class/193185/4 "2010-08-12T19:40:11Z")

</div>

Thanks for your replies.

Using columns, the name can be queried by human\_name:

Art.columns[0].human\_name

inspect is perfect for displaying purposes.

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 13, 2010, 9:37pm UTC](https://www.ruby-forum.com/t/how-to-query-the-schema-of-a-activerecord-class/193185/5 "2010-08-13T21:37:38Z")

</div>

Fritz T. wrote:

> Thanks for your replies.
> 
> Using columns, the name can be queried by human\_name:
> 
> Art.columns[0].human\_name
> 
> inspect is perfect for displaying purposes.

Note that, in my experience at least, this is rarely necessary unless  
you’re writing something like ActiveScaffold. What are you trying for  
here?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
