# Re: ActiveRecord Question -- making arrays or hashs out of database tables

**URL:** <https://www.ruby-forum.com/t/re-activerecord-question-making-arrays-or-hashs-out-of-database-tables/147019>\
**Category:** Ruby\
**Created:** [August 27, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/re-activerecord-question-making-arrays-or-hashs-out-of-database-tables/147019 "2008-08-27T16:53:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [August 27, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/re-activerecord-question-making-arrays-or-hashs-out-of-database-tables/147019/1 "2008-08-27T16:53:05Z")

</div>

Thanks for your reply.Â This is very helpful.  
Glenn

----- Original Message ----  
From: Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, August 20, 2008 2:55:07 PM  
Subject: Re: ActiveRecord Question – making arrays or hashs out of  
database tables

Glenn wrote:

> Better yet, I’d like to be able to create an array of hashes, where each  
> element of the array is equal to a hash of the fields in the table, like  
> this:  
> [{:x =\> 1, :y =\> ‘a’, :z =\> 1.1}, {:x =\> 2, :y =\> ‘b’, :z =\> 6.2}, {:x  
> =\> 3, :y =\> ‘c’, :z =\> 0.001}]

Yes, you can do that.  
I have a ‘clients’ table which is represented by a ‘Client’ model (maybe  
in a rails application).  
I can do  
Â clients=Client.find(:all).collect {|c| c.attributes }  
and access it like  
Â clients.each do |client|  
Â Â client[‘first\_name’]Â # do something with first\_name field value  
Â end  
which gives you your array of hashes.  
However,  
Â clients=Client.find(:all)  
will give you  
Â clients.each do |client|  
Â Â client.first\_name  
Â end  
which is nicer and you get all the power of active record as a result,  
because ‘client’ is an instance of Client and not just a hash.

> Can anyone tell me if this is doable in ActiveRecord, or in some other  
> Ruby package, or with some stand-alone Ruby code?

Someone might shoot me down here, but I’d say that ActiveRecord is good  
for working with small updates and selects as in the sort of thing you  
might do with a web app interface.  
If you’re not using rails, then it is quite likely you might look at  
alternatives.Â There are drivers for various databases which will fetch  
data into arrays and hashes, and their are also alternatives to  
activerecord which do ORM.Â Haven’t been using a lot of them lately and  
it may depend on the database.

Regards,  
Daniel
