hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Ezra's Where Plugin is Out #

by why in inspect

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.

said on 31 Jan 2006 at 13:09

Daaaamn… someone remind me why we still use SQL ?

said on 31 Jan 2006 at 13:13

I think I like that author.any, there is some serious ruby magic going on these days.

said on 31 Jan 2006 at 13:41

Whee! Lovely lovely.

said on 31 Jan 2006 at 15:27

Wow, great. This is so needed.

Really, this kind of stuff is the future.

said on 31 Jan 2006 at 15:55

Why not write a couple of lines and have ez_find use Criteria instead of implementing your own?

said on 31 Jan 2006 at 16:24

This seems very much like Kirbybase, no?

said on 01 Feb 2006 at 00:35

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.

said on 01 Feb 2006 at 01:47

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 :)

said on 01 Feb 2006 at 02:31

riffraff: I believe this is a plugin for ActiveRecord. It just makes the where clause easier to work with.

said on 01 Feb 2006 at 02:57

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 .

said on 01 Feb 2006 at 06:44
danail: The point is you’ve got all the components of your program integrated under the ruby umbrella. When you execute this query, you can figure out what columns are expected, how many tables are being joined, or any other useful bit of knowledge without having to parse sql (a huge pain I assure you). This makes doing things like unit testing and integration work light years easier. The whole point being Ruby (and especially Rails) does the real work for you by generating the actual environment/code, you just tell it what you want. Not only is it a labor saving technique, but it generally reduces error since the implementation is one step higher up and closer to the design. This is IMHO why stuff like domain specific languages are the bees knees.
said on 01 Feb 2006 at 06:45

ps: dang it if these lucky charms don’t get me on a sugar rush :P

said on 01 Feb 2006 at 14:36

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!

said on 01 Feb 2006 at 14:41
Oh and one more little example that lets you use ar objects inside the conditions ;)
ezra = Author.find(session[:user_id])   
# all articles written by Ezra
articles = Article.ez_find(:all, :include => :author) do |article, author|
  author << ezra # use AR instance to add condition; uses PK value if set: author.id = ezra.id
end
said on 02 Feb 2006 at 10:33

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.

said on 02 Feb 2006 at 10:39

Sorry about that. They upgraded the memory on my VPS and started lighty with the wrong conf file for me. Its fixed now.

said on 04 Feb 2006 at 21:03

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 ;)

said on 10 Feb 2006 at 13:33

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… ;-)

said on 23 Mar 2006 at 15:34

asdf

Comments are closed for this entry.