---
title: Squirrel - Once More, with Feeling!
teaser: The second release of our Squirrel library for Ruby SQL queries.
tags: web,rails,squirrel
author: Jon Yurek
published_on: 2007-01-12
---

I admit, the first iteration of [Squirrel] wasn't really as super fabulous as I
thought it would be. Because of how I was using the blocks, you couldn't do a
simple thing like access `params`. And since half the fun of it was being able
to make good looking, flexible queries, not being able to use `params` was a
giant pain.

[Squirrel]: https://thoughtbot.com/blog/squirrel-natural-looking-queries-for-rails

Now, however, I've managed to mix the clean, functional look of `instance_eval`
with the husky utilitarianism of `params` to come up with a happier, shinier
Squirrel:

```ruby
users = User.find(:all) do
  blog.title == params[:blog_name]
end
```

And what's that in the trees, gracefully swinging from vine to vine? It's our
friends `any` and `all` here to give us grouping blocks!

```ruby
site = Site.find(:first) do
  any {
    domains.hostname == params[:hostname]
    domains.hostname == "www.#{params[:hostname]}"
  }
end
```

All the functions you'd expect to be able to use in your controller are
available (well, as long as you actually _are_ in your controller, anyway;
squirrels aren't miracle workers) params, session, etc.

You can use the unary minus to negate any condition or block, and you can also
use it to do a descending order\_by.

```ruby
Post.find(:all) do
  -any {
    title =~ /^OMG/
    id == 4
  }
  order_by -created_on
end
```

This is a major rewrite from the original code and cleaned up things in a very
good way. And what's more, we're using it in real code now, so I have a fire
under my ass to keep it in fighting shape.

The repo is [here][repo] for all of you itching to try it out.

[repo]: https://github.com/thoughtbot/squirrel
