From: avi@...4.com (Avi Bryant) Date: 2002-05-20T08:48:39+09:00 Subject: Re: Object <-> Relational mapping Thomas Hurst wrote in message news:<20020519165132.GA52065@voi.aagh.net>... > I'm getting very, very tired of writing code that looks like this: > > db.select_all(long_sql_statement) do |row| > foo = Foo.new > foo.a = row['a'] > foo.b = row['b'].to_i > foo.c = row['c'].to_f > #... > foo.z = row['z'] > end > > It gets even worse when you get relations etc to worry about. > > I've heard of at least two mentions of people writing object-relational > mappers, and I'm sure plenty of you lot have written db heavy apps. I played with OR mapping in Ruby a long time ago, but mostly just as an experiment. You might find something of interest in my code, which I think is still available at http://beta4.com/kansas.tgz (it assumes MySQL - I think Eli Green has a version of this that works with Ruby/DBI as well). I wouldn't expect you to be able to use it directly, however, at least not without a fair amount of tweaking. One of the more interesting things it did was use blocks to generate sql queries - that is, you'd do something like x = 'asd%' Foo.select{|foo| (foo.a == 1) & (foo.bar.c =~ x)} rather than SELECT * FROM Foo, Bar WHERE Foo.a = 1 AND Foo.bar_id = Bar.id AND Bar.c LIKE 'asd%'. Cheers, Avi