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

**URL:** <https://www.ruby-forum.com/t/activerecord-question-making-arrays-or-hashs-out-of-database-tables/146413>\
**Category:** Ruby\
**Created:** [August 20, 2008, 8:26pm UTC](https://www.ruby-forum.com/t/activerecord-question-making-arrays-or-hashs-out-of-database-tables/146413 "2008-08-20T20:26:13Z")\
**Posts on this page:** 3\
**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 20, 2008, 8:26pm UTC](https://www.ruby-forum.com/t/activerecord-question-making-arrays-or-hashs-out-of-database-tables/146413/1 "2008-08-20T20:26:13Z")

</div>

Hi,  
I have a question about using ActiveRecord.Â The way I understand  
ActiveRecord is that you can use it as a kind of bridge between Ruby and  
a relational database.Â I believe that you can use it to create objects  
from tables and that the objects are equivalent to the rows of data in  
the tables.  
I want to be able to use it a bit differently than that.Â I want to be  
able to create abjects that are arrays of the values in specific fields  
in a given table.Â For example, given the following table:  
XÂ Â Â YÂ Â Â Z  
1Â Â 'a’Â Â 1.1  
2Â Â 'b’Â Â 6.2  
3Â Â 'c’Â Â 0.001  
I want to be able to create an array from the X field that is equal to  
[1, 2, 3], and an array from the Y field that is equal to [‘a’, ‘b’,  
‘c’], and so forth.

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}]  
Can anyone tell me if this is doable in ActiveRecord, or in some other  
Ruby package, or with some stand-alone Ruby code?  
Thanks for your help,  
Glenn

---

<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, 5:30pm UTC](https://www.ruby-forum.com/t/activerecord-question-making-arrays-or-hashs-out-of-database-tables/146413/2 "2008-08-27T17:30:02Z")

</div>

On Wed, Aug 20, 2008 at 8:21 PM, Glenn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

I believe this is overkill for an ORM, you can just use the DBI  
interface for example:

```
require 'rubygems'
require 'dbi'

DBI.connect('dbi:SQLite3:data.db') do |dbh|
  x = dbh.select_all("select x from data")
  y = dbh.select_all("select y from data")
  z = dbh.select_all("select z from data")

  table = dbh.select_all("select * from data").map {|row| row.to_h}
end

```

The example uses SQLite, but the same code may be used for many others  
with a DBD driver, see:

[http://ruby-dbi.rubyforge.org/rdoc/index.html](http://ruby-dbi.rubyforge.org/rdoc/index.html)

Just changing the connection string ‘dbi:SQLite3:data.db’.

---

<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 20, 2008, 8:58pm UTC](https://www.ruby-forum.com/t/activerecord-question-making-arrays-or-hashs-out-of-database-tables/146413/3 "2008-08-20T20:58:45Z")

</div>

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
