From: Glenn Date: 2008-08-27T23:47:43+09:00 Subject: Re: ActiveRecord Question -- making arrays or hashs out of database tables --0-1753033728-1219848663=:63779 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Thanks for your reply.=A0 This is very helpful.=0AGlenn=0A=0A=0A=0A----- Or= iginal Message ----=0AFrom: Daniel Bush =0ATo: ruby-ta= lk ML =0ASent: Wednesday, August 20, 2008 2:55:07 = PM=0ASubject: Re: ActiveRecord Question -- making arrays or hashs out of da= tabase tables=0A=0AGlenn wrote:=0A=0A> =0A> Better yet, I'd like to be able= to create an array of hashes, where each =0A> element of the array is equa= l to a hash of the fields in the table, like =0A> this:=0A> [{:x =3D> 1, :y= =3D> 'a', :z =3D> 1.1}, {:x =3D> 2, :y =3D> 'b', :z =3D> 6.2}, {:x =0A> = =3D> 3, :y =3D> 'c', :z =3D> 0.001}]=0A=0AYes, you can do that.=0AI have a = 'clients' table which is represented by a 'Client' model (maybe =0Ain a rai= ls application).=0AI can do=0A=A0 clients=3DClient.find(:all).collect {|c| = c.attributes }=0Aand access it like=0A=A0 clients.each do |client|=0A=A0 = =A0 client['first_name']=A0 # do something with first_name field value=0A= =A0 end=0Awhich gives you your array of hashes.=0AHowever,=0A=A0 clients=3D= Client.find(:all)=0Awill give you=0A=A0 clients.each do |client|=0A=A0 =A0 = client.first_name=0A=A0 end=0Awhich is nicer and you get all the power of a= ctive record as a result, =0Abecause 'client' is an instance of Client and = not just a hash.=0A=0A=0A> Can anyone tell me if this is doable in ActiveRe= cord, or in some other =0A> Ruby package, or with some stand-alone Ruby cod= e?=0A=0ASomeone might shoot me down here, but I'd say that ActiveRecord is = good =0Afor working with small updates and selects as in the sort of thing = you =0Amight do with a web app interface.=0AIf you're not using rails, then= it is quite likely you might look at =0Aalternatives.=A0 There are drivers= for various databases which will fetch =0Adata into arrays and hashes, and= their are also alternatives to =0Aactiverecord which do ORM.=A0 Haven't be= en using a lot of them lately and =0Ait may depend on the database.=0A=0ARe= gards,=0ADaniel=0A-- =0APosted via http://www.ruby-forum.com/. --0-1753033728-1219848663=:63779--