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

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

On Wed, Aug 20, 2008 at 8:21 PM, Glenn [email protected] 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

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

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