# \[ANN\] Slice and dice plugin

**URL:** <https://www.ruby-forum.com/t/ann-slice-and-dice-plugin/58522>\
**Category:** Rails\
**Created:** [April 22, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/ann-slice-and-dice-plugin/58522 "2006-04-22T16:26:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [April 22, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/ann-slice-and-dice-plugin/58522/1 "2006-04-22T16:26:59Z")

</div>

I’ve been playing around with some easier ways of specifying  
conditions in active record, and have come up with a little plugin  
that some may find useful. Currently active record finder and  
calculations methods can take a :conditions option with a string or  
array listing some sql conditions, eg:

Animals.find :first, :conditions =\> “name = ‘Tiger’”

My plugin enables simple conditions like this to be written without  
any SQL at all, using a hash instead of an array or string:

Animals.find :first, :conditions =\> {:name =\> ‘Tiger’}

As well as simple equality conditions, other tests such as more than,  
less than and contains can also be specified:

Animals.find :first, :conditions =\> {:name\_starts\_with =\> ‘Tig’}  
Animals.find :all, :conditions =\> {:population\_more\_than =\> 1\_000\_000}

Conditions can be combined:

endangered\_tigers = Animals.find :all, :conditions =\> {:name\_contains  
=\> ‘Tiger’, :population\_less\_than =\> 10\_000}

These conditions can also be used in calculations and scoped queries:

Animals.count :conditions =\> {:population\_more\_than =\> 1\_000\_000}

Animals.with\_scope :find =\> {:conditions =\> {:name\_contains =\> ‘Tiger’}}  
do  
Animals.find :all  
end

Finally, here’s an simple way to use this plugin to add lots of data  
slicing and dicing to any list actions you might have:

class AnimalController \< ActiveController  
def list  
animal\_conditions = params.dup.reject! {|k, v| [:action, :control  
ler].include? k}  
@animals = Animal.find :all, :conditions =\> animal\_conditions  
end  
end

This allows urls such as  
/animal/list?name\_starts\_with=T&population\_less\_than=1000

Please take a look, the code is available at  
svn://rubyforge.org/var/svn/popdog/slice\_and\_dice/tags/REL-0.1

I’d welcome any comments or ideas on how to improve this (currently  
very simple) plugin. I do have some planned extra features of my own,  
so watch this space!

Tom

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [April 22, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/ann-slice-and-dice-plugin/58522/2 "2006-04-22T18:15:12Z")

</div>

On Apr 22, 2006, at 7:24 AM, Tom W. wrote:

> ## Tom

Hey Tom-

```
Just wanted to point you at my plugin called ez_where:

```

[http://brainspl.at/articles/2006/01/30/i-have-been-busy](http://brainspl.at/articles/2006/01/30/i-have-been-busy)  
[http://opensvn.csie.org/ezra/rails/plugins/dev/ez\_where/](http://opensvn.csie.org/ezra/rails/plugins/dev/ez_where/)

```
My plugin solves the same problems that slice and dice does but in a

```

different way. It allows you to use ruby operators instead of sql ops  
and it allows find to take a block to define :conditions. It also  
allows for nested sub conditions and stuff like that. Here’s a little  
peak at what it can do:

@articles = Article.find\_with\_block(:all, :include =\> :author) do |  
article, author|  
article.title =~ “%Foo Title%”  
author.any do  
name == ‘Ezra’  
name == ‘Fab’  
end  
end

This will do a find :all with:  
:conditions =\> [“article.title LIKE ? AND  
(authors.name = ? OR authors.name = ?)”,  
“%Foo Title%”, “Ezra”, “Fab”]

Basically here is the breakdown of how we map ruby operators  
to SQL operators:

foo == ‘bar’ #=\> [“foo = ?”, ‘bar’]  
foo =~ ‘%bar’ #=\> [“foo LIKE ?”, ‘%bar’]  
foo \<=\> (1…5) #=\> [“foo BETWEEN ? AND ?”, 1, 5]  
id === [1, 2, 3, 5, 8] #=\> [“id IN(?)”, [1, 2, 3, 5, 8]]  
\<, \>, \>=, \<= et all will just work like you expect.

There is also the ability to create the conditions in stages so  
you can build up a query:

cond = Caboose::EZ::Condition.new do  
foo == ‘bar’  
baz \<=\> (1…5)  
id === [1, 2, 3, 5, 8]  
end

@result = Model.find(:all, :conditions=\> cond.to\_sql)  
#=\> [“foo = ? AND baz BETWEEN ? AND ? AND id IN (?)”,  
“bar”, 1, 5, [1, 2, 3, 5, 8]]

```
There is some seriously fun metaprogramming going on in ez_where

```

that you might want to take a peek at 😉

Cheers-  
-Ezra

PS. the test cases are 700 lines long and serve as the best docs  
right now. The tests exercise all the features of the plugin.

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [April 23, 2006, 5:32am UTC](https://www.ruby-forum.com/t/ann-slice-and-dice-plugin/58522/3 "2006-04-23T05:32:22Z")

</div>

On Apr 22, 2006, at 2:27 PM, Craig W. wrote:

> > > 
> > 
> > There is some seriously fun metaprogramming going on in ez\_where that
> 
> * * *
> 
> You beat me to the punch as I was about the say the same thing…as  
> you know, I very much love ez\_where and have made good usage of it.
> 
> I can’t figure out how to determine with version I have and whether I  
> should update it, nor can I tell when you update the plugin itself.
> 
> Craig

Craig-

```
ez_where is at revision 85 right now and hasn't changed in about a

```

month. I am working on an update for it with a few more niceties that  
I will roll out soon and I will be sure to let you know.

Cheers-

Ezra

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [April 28, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/ann-slice-and-dice-plugin/58522/4 "2006-04-28T16:33:02Z")

</div>

That’s really cool, Tom. I love how readable your queries are.

Duane J.  
(canadaduane)  
[http://blog.inquirylabs.com/](http://blog.inquirylabs.com/)

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [April 22, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/ann-slice-and-dice-plugin/58522/5 "2006-04-22T23:31:01Z")

</div>

On Sat, 2006-04-22 at 09:12 -0700, Ezra Z. wrote:

> > I’d welcome any comments or ideas on how to improve this (currently  
> > Hey Tom-
> 
> Just wanted to point you at my plugin called ez\_where:
> 
> [Ruby on Rails Blog / What is Ruby on Rails for?](http://brainspl.at/articles/2006/01/30/i-have-been-busy)  
> [http://opensvn.csie.org/ezra/rails/plugins/dev/ez\_where/](http://opensvn.csie.org/ezra/rails/plugins/dev/ez_where/)

> now. The tests exercise all the features of the plugin.

* * *

You beat me to the punch as I was about the say the same thing…as you  
know, I very much love ez\_where and have made good usage of it.

I can’t figure out how to determine with version I have and whether I  
should update it, nor can I tell when you update the plugin itself.

Craig
