Ezra's Where Plugin is Out #
Okay, well, KLAAABOOM, more Ruby. For spelling out queries. Lotsa operator mischief. As always, don’t take this too seriously, if you are prone to it. If you are not, then this calls for some bubbly.
articles = Article.ez_find(:all, :include => :author) do |article, author| article.title =~ "%Foo Title%" author.any do name 'Ezra' name ‘Fab’ end end
To install:
script/plugin install http://opensvn.csie.org/ezra/rails/plugins/dev/ez_where/
And, if you go deeper, there is a whole explanation.
Danno
Daaaamn… someone remind me why we still use SQL ?
netghost
I think I like that author.any, there is some serious ruby magic going on these days.
pmcm
Whee! Lovely lovely.
MenTaLguY
Wow, great. This is so needed.
Really, this kind of stuff is the future.
evan
Why not write a couple of lines and have ez_find use Criteria instead of implementing your own?
James
This seems very much like Kirbybase, no?
Matt Todd
Don’t laugh, but I’m working on implementing this kind of stuff (and the magic in ActiveRecord) in PHP5 for my own framework. It’s more difficult than it looks, particularly when developing in PHP5 . Oh to remove constraints!
M.T.
riffraff
seems cool, but I’d like to know why they did’nt just rip off lafcadio, and how they handled !=.
Anyway, congrats for this work and for writing nice docs for it :)
netghost
riffraff: I believe this is a plugin for ActiveRecord. It just makes the where clause easier to work with.
Danail Nachev
I tried to understand why we need html generation code. OK. I think this gives us the ability to produce valid HTML more easily. But why we need such thing for SQL . SQL is rather simple language which don’t need such assistance. Errors in SQL is seen immediately and you can correct them. I don’t think that the code also is becomming more clear. I think it is more vague this way. The only thing which you get with is that you don’t need to be expert in SQL to write SQL .
fansipans
fansipans
ps: dang it if these lucky charms don’t get me on a sugar rush :P
Ezra
Initially this library started as an answer I gave to someone on the rails list about how to dynamically build the :conditions for a search on params. But it was fun to play with so I kept working on it. Danail, a lot of people have that reaction on first looking at this stuff. Why don’t you just use sql? SQL is pretty easy to write. But the whole thing that attracts me to rails is the beauty of the code and the DSL it provides for the problem domain of databse driven web apps. So this is just my little attempt to bring a little more beauty to my rails code. I hate to look at my controllers and models and see the dis-harmony between nice looking ruby code and then ugly sql statements mixed together. If you like this kind of thing, you can get your rails code to look mostly all like ruby now and I just find that appealing. And another reason I did this is beacause I can and it was there ;-) It was a nice exercise in a bit of metaprogramming madness that ruby is so great at. Aside from all that, there are still some issues I need to work out with the plugin. So I am asking for feedback on the syntax and for suggestions of how to make a few parts of it better. The code is well commented and well tested so anyone who feels like adding something, I love patches;) The != part is one of the last warts in this plugin. That is part that will definitely be in flux over the next few iterations. Any suggestion for a better way of doing that would be welcome. And the reason I didn’t use Criteria is mainly that I wanted to do it myslef for the experience. And I didn’t quite like a few things about criteria’s syntax. PLus it was more heavyweight solution then I needed. All this plugin does is build the :conditions clause and send it along iots merry way to AR::Base.find. No muss no fuss. So its quick, it just builds a fgew arrays with strings in them . So it is not noticably slower then a normal AR::Base.find at all. In fact most of this code could be used outside rails easily to build the wehre clause for other systems. I am talking to George about some nitro/ogg integration of this code. Anyway thank you for the kind workds people. Please abuse it as hard as you can and give me some feedback please!
Ezra
CptOatmeal
Brainspl.at has gone kablooie! The link to the article returns a 404 and the main domain (http://brainspl.at) gives me the raw php code of the site.
Ezra
Sorry about that. They upgraded the memory on my VPS and started lighty with the wrong conf file for me. Its fixed now.
Jaykul
I’m no Ruby wiz, but … it seems to me that you’re trying to force ruby semantics on a SQL query, just to get syntax checking.
While I’m a big fan of syntax checking in my tools, I think the code would be clearer (at least for those of us more used to SQL than to Ruby) to bring in a SQL —like syntax to Ruby?
Essentially this is what Microsoft’s LINQ does for C#, but the fact that it was their idea shouldn’t discourage us from doing the same thing with other languages ;)
ConsultUtah
This is similar to what I did in my C# DAL - started long before I heard about ruby or rails:
List Customer cs = Customer.FindBy(Customer.FirstName(“Jeff”), Customer.SalesToDate(10000,Operator.GT),Customer.Limit(5));
Everything is statically typed.
Don’t hate me! I like ror lots… ;-)
test
asdf
Comments are closed for this entry.