This is the first release of a new plugin for making very Rubyish
queries. I never really liked AR’s find method syntax, and all the
replacements I’ve seen didn’t make it any easier to join on multiple
models at the same time, so I wrote Squirrel.
posts = Post.find do
user.email =~ “%thoughtbot%”
tags.name === %w( ruby rails )
created_on <=> [ Time.now - 2.weeks, Time.now - 1.week ]
end
It overloads the base find method to take a block to build conditions,
including any and all association joins. You reference them like you
would in a normal block of Ruby.
You reference associations by whatever name you gave to it in the
has_many, belongs_to, etc. You can then access all the columns and
relationships on that model. It is rather specific, though, and will
raise errors if you misspell your relationships or donâ??t pluralize
right.
And you simply reference columns by their normal names and use any of
==, ===, <=>, =~, >=, <=, >, and < on them pretty much like youâ??d expect
to be able to (before you ask, yes, I cribbed the syntax from ez_where,
since it makes sense). It handles nil values in == with a quick trip to
IS NULL and it handles negation of conditions through the unary -.
It’s not as full featured as I’d like it – yet, but IMHO it looks a lot
better, and it’s still useful.
You can read further about it at
http://giantrobots.thoughtbot.com/2006/9/29/an-improvement-for-querying-in-rails
Or you can install it via script/plugin install
http://svn.thoughtbot.com/plugins/squirrel
Jon Yurek
http://www.thoughtbot.com