Ambition - AR Adapter to write db queries in Ruby
ambition.rubyforge.org
ambition.rubyforge.org
What am I missing? Is this just for people who need to do "SELECT * FROM comments WHERE articleid=?" and don't want to learn even that much SQL?
What are you missing?
It's not that user wouldn't know SQL. It's that code like this is more readable because you don't do a mental switch in your brain. I'm all for it: see my story [1] for a take on readability.
Or just accept the fact that there are tools around for mature people who enjoy it. DHH didn't create his ORM for dumb people, he did it because it made his hackin' universe more pleasurable. Some dumb people used it later, but who cares.
It's not that I've decided it will leak, it's that I can never quite trust that it won't. I would have to continually prove to myself that my code is right. It's a weakness of mine that if I don't 100% understand the code I'm writing, I worry and that slows me down. That code becomes something I can't quite get out of the back of my mind.
Also, I don't buy your readability argument. I can read and think in SQL just fine. Yes, SQL can be complicated, but only when the problem requires it. It maps very directly and naturally on to the problem of querying. There isn't much in SQL that isn't necessary. There isn't much in SQL that is missing. It's already as simple as can be. To make it any simpler makes it worse. Mixing the asking of a question with the processing of the answer makes both parts harder to understand.
Perhaps it is easier to read for some people, but I think that's likely because they just aren't very good with SQL yet. So, this seems to me to make code less readable and more difficult to reason about. I don't see the advantage.
Btw I actively use SQL since 1997 so I guess I have quite a proficiency. Still my point of readability wins, at least for me. So you see, your argument about readability doesn't hold for some people. Mine too, but I don't complain.
One major difference with Ambition is that you're writing the queries in the same code you would when using the normal Enumerable API. IMHO from an interface pov, it's better than any Ruby ORM has provided (or would ever be able to). However, the (big) downside is that it requires ParseTree, which only works on Ruby 1.8. It's also a bit fragile, since if you use code the underlying parser doesn't understand you'll get weird runtime errors.
I created an Ambition-like interface to DataMapper early last year, which uses a similar approach:
http://github.com/dkubb/dm-ambition/
The main difference (aside from working with DataMapper) is that when the underlying collection is already lazy loaded the block provided is evaluated for each record using the normal Enumerable methods. I believe the Ambition project has a few constructs that make this impossible, but in dm-ambition I set this as a requirement from the start.My hacker colleague who lives on the edge with DataMapper says Ambition-like stuff is to be included in DM. Is that related to your gem or a completely different activity?
Or, if you're in Rails 3, just use Arel, which is part of ActiveRecord.