ActiveRecord Question -- making arrays or hashs out of databasetables

G

Glenn

Hi,=0AI have a question about using ActiveRecord.=A0 The way I understand A=
ctiveRecord is that you can use it as a kind of bridge between Ruby and a r=
elational database.=A0 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 tabl=
es.=0AI want to be able to use it a bit differently than that.=A0 I want to=
be able to create abjects that are arrays of the values in specific fields=
in a given table.=A0 For example, given the following table:=0AX=A0=A0=A0 =
Y=A0=A0=A0 Z=0A1=A0=A0 'a'=A0=A0 1.1=0A2=A0=A0 'b'=A0=A0 6.2=0A3=A0=A0 'c'=
=A0=A0 0.001=0AI want to be able to create an array from the X field that i=
s equal to [1, 2, 3], and an array from the Y field that is equal to ['a', =
'b', 'c'], and so forth.=0A=0ABetter 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:=0A[{:x =3D> 1, :y =3D> 'a', :z =3D> 1.1}, {=
:x =3D> 2, :y =3D> 'b', :z =3D> 6.2}, {:x =3D> 3, :y =3D> 'c', :z =3D> 0.00=
1}] =0ACan anyone tell me if this is doable in ActiveRecord, or in some oth=
er Ruby package, or with some stand-alone Ruby code?=0AThanks for your help=
,=0AGlenn
 
D

Daniel Bush

Glenn said:
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
 
X

Xavier Noria

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,

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'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top