# Getting column names from a record set

**URL:** <https://www.ruby-forum.com/t/getting-column-names-from-a-record-set/87458>\
**Category:** Rails\
**Created:** [March 2, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/getting-column-names-from-a-record-set/87458 "2007-03-02T18:26:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [March 2, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/getting-column-names-from-a-record-set/87458/1 "2007-03-02T18:26:06Z")

</div>

Hello,

I’m trying to get the column names associated w/ this record set:

account\_all = Account.find(:all)

Since account\_all is an array - how do I get the names?

I know there’s got to be a simple way to do this.

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [March 2, 2007, 6:33pm UTC](https://www.ruby-forum.com/t/getting-column-names-from-a-record-set/87458/2 "2007-03-02T18:33:56Z")

</div>

On 3/2/07, Clem R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> I’m trying to get the column names associated w/ this record set:
> 
> account\_all = Account.find(:all)

Since account\_all is an array - how do I get the names?

account\_all.first.attributes.keys

ed

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [March 2, 2007, 6:52pm UTC](https://www.ruby-forum.com/t/getting-column-names-from-a-record-set/87458/3 "2007-03-02T18:52:57Z")

</div>

> I’m trying to get the column names associated w/ this record set:
> 
> account\_all = Account.find(:all)
> 
> Since account\_all is an array - how do I get the names?

Account.column\_names

-philip

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [March 2, 2007, 6:53pm UTC](https://www.ruby-forum.com/t/getting-column-names-from-a-record-set/87458/4 "2007-03-02T18:53:57Z")

</div>

The column names are a property of the model, in this case Account, so  
you  
can try something like:

account\_all = Account.find(:all)  
account\_all.each do |account|  
for col in Account.content\_columns  
col.human\_name: account.send(col.name)  
end  
end

Here’s a good example of handling columns/rows:  
[http://wiki.rubyonrails.org/rails/pages/HowToExportToExcel](http://wiki.rubyonrails.org/rails/pages/HowToExportToExcel)

Regards,  
Dave

* * *

Information and Educational Technology  
Kwantlen University College - 604-599-2120  
“So powerful is the light of unity that it can illuminate the whole  
earth.” --BahÃ¡'u’llÃ¡h

Clem R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent by: removed\_email\_address@domain.invalid  
02/03/2007 09:26 AM  
Please respond to  
removed\_email\_address@domain.invalid

To  
removed\_email\_address@domain.invalid  
cc

Subject  
[Rails] Getting column names from a record set

Hello,

I’m trying to get the column names associated w/ this record set:

account\_all = Account.find(:all)

Since account\_all is an array - how do I get the names?

I know there’s got to be a simple way to do this.

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [March 2, 2007, 7:13pm UTC](https://www.ruby-forum.com/t/getting-column-names-from-a-record-set/87458/5 "2007-03-02T19:13:41Z")

</div>

Perfect - exactly what I wanted!

Thanks!

Ed Hickey wrote:

> On 3/2/07, Clem R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hello,
> > 
> > I’m trying to get the column names associated w/ this record set:
> > 
> > account\_all = Account.find(:all)
> 
> Since account\_all is an array - how do I get the names?
> 
> account\_all.first.attributes.keys
> 
> ed
