# \[ANN\] ez\_where plugin updated features

**URL:** <https://www.ruby-forum.com/t/ann-ez-where-plugin-updated-features/99241>\
**Category:** Rails\
**Created:** [January 31, 2006, 4:35am UTC](https://www.ruby-forum.com/t/ann-ez-where-plugin-updated-features/99241 "2006-01-31T04:35:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ezra\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f07891/32.png) [@ezra\_z](https://www.ruby-forum.com/u/ezra_z)\
**Post date:** [January 31, 2006, 4:35am UTC](https://www.ruby-forum.com/t/ann-ez-where-plugin-updated-features/99241/1 "2006-01-31T04:35:09Z")

</div>

Friends-

```
I wanted to let people know that there is a new experimental release

```

of this plugin. I would love feedback on syntax and features. There  
is now a full test suite with fixtures that covers all the available  
syntax. Look at the test suite for more syntax possibilities. There  
have been many additions since my last release. Fabien Atelier has  
been working on this with me and has added a bunch of great code.

```
You can get it from here:

```

[http://http](http://http)://brainspl.at/opensvn.csie.org/ezra/rails/plugins/dev/  
ez\_where/

```
This is a request for feedback. I want to solidify the api soon so

```

suggestions are welcome.

Here’s a snippet from the README:

Welcome to the new improved ez\_where plugin for rails. This plugin is  
meant  
to be used as a nice ruby like syntax for creating the :conditions  
part of an  
ActiveRecord::Base.find. We also add the ActiveRecord::Base.ez\_find  
method.  
This method takes a block to simplify single and multi table queries.  
There  
is now support for nested sub conditions using a few different  
techniques. The  
default boolean used is ‘AND’. any takes a subcondition block and  
uses ‘OR’  
to join them together.

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

This issue a find with these :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.

And here is a link to my blog with the entire README for your perusal:

[http://brainspl.at/articles/2006/01/30/i-have-been-busy](http://brainspl.at/articles/2006/01/30/i-have-been-busy)

Cheers-  
-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![ezra\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f07891/32.png) [@ezra\_z](https://www.ruby-forum.com/u/ezra_z)\
**Post date:** [January 31, 2006, 6:43am UTC](https://www.ruby-forum.com/t/ann-ez-where-plugin-updated-features/99241/2 "2006-01-31T06:43:44Z")

</div>

Nice idea Ezra! Will play with it when I can.

b

---

<div class="post-metadata">

**Author:** ![ezra\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f07891/32.png) [@ezra\_z](https://www.ruby-forum.com/u/ezra_z)\
**Post date:** [January 31, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/ann-ez-where-plugin-updated-features/99241/3 "2006-01-31T22:05:43Z")

</div>

Harvey-

```
Thanks for testing this out for me. It will be easy to do what you

```

want. The problem you are having relates to the use of instance vars  
like @postcode\_var. Since the condition is getting evaluated inside  
the Caboose::EZ::Condition block, @postcode\_var won’t work and will  
return the sting you wanted as ‘%%’. The way around this is to just  
use plain old local vars. SO what you want to do can be done like this:

postcode\_var = params[:home\_search][:postcode\_area]

cond = Caboose::EZ::Condition.new do  
postcode =~ “%#{postcode\_var}%”  
end

@record\_count = Home.count(cond.to\_sql)

```
Also you could do the same thing like this:

```

cond = Home.ez\_condition { postcode =~ “%#{postcode\_var}%” }

@record\_count = Home.count(cond.to\_sql)

```
I need to add to the docs the fact that you can't use @instance vars

```

inside the conditions as they are not evaluated in the correct  
context. Try this solution out and see how it works for you and let  
me know.

Cheers-  
-Ezra

On Jan 31, 2006, at 11:41 AM, Harvey B. wrote:

> The problem I am having is building the where condition. I cannot
> 
> # Search for a home using the information entered in index.
> 
> ```
> end
> 
> ```
> 
> Processing HomeController#search (for 127.0.0.1 at 2006-01-31  
> (postcode LIKE ‘%%’) e[0m  
> Rendered shared/\_list\_object (0.00250)  
> Rendered shared/\_list\_object (0.00248)  
> (0.00587) Rendered shared/\_top\_menu (0.00289) Rendered  
> Harvey
> 
> > now a full test suite with fixtures that covers all the available
> > 
> > This method takes a block to simplify single and multi table queries.  
> > name == ‘Fab’
> > 
> > This e-mail has been scanned for all viruses by MessageLabs.
> 
> > Welcome to the new improved ez\_where plugin for rails. This plugin is  
> > uses ‘OR’
> > 
> > foo \<=\> (1…5) #=\> [“foo BETWEEN ? AND ?”, 1, 5]  
> > WebMaster  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

-Ezra Z.  
Yakima Herald-Republic  
WebMaster

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ezra\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f07891/32.png) [@ezra\_z](https://www.ruby-forum.com/u/ezra_z)\
**Post date:** [January 31, 2006, 8:41pm UTC](https://www.ruby-forum.com/t/ann-ez-where-plugin-updated-features/99241/4 "2006-01-31T20:41:54Z")

</div>

This plugin seems to be exactly what I am looking for. However, IÂ¹ve  
been struggling this afternoon to get it to work. I should point out  
that I am a newbie to Rails. IÂ¹ve created a search page using a model  
which does not have a corresponding table, as outlined here  
([http://rails.techno-weenie.net/tip/2005/11/19/validate\_your\_forms\_with\_a\_ta](http://rails.techno-weenie.net/tip/2005/11/19/validate_your_forms_with_a_ta)  
ble\_less\_model). This bit works perfectly as the simple validation  
rules  
work as expected.

The problem I am having is building the where condition. I cannot seem  
to get the right syntax to get hold of the params value and build the  
condition. IÂ¹m just trying to build a very simple example. I have a  
textfield called postcode\_area and all I want to do is create the  
following select (SELECT \* FROM homes WHERE postcode LIKE  
‘%postcode\_area%’

The code in the controller is as follows:-

# Search for a home using the information entered in index.

def search  
@home\_search = HomeSearch.new(params[:home\_search])

if @home\_search.valid?  
@postcode\_var = params[:home\_search][:postcode\_area]

```
  condition = Caboose::EZ::Condition.new do
    postcode =~ "%#{@postcode_var}%"
   # postcode =~ "%G77%"
  end

  @record_count = Home.count(condition.to_sql)
  if @record_count == 0
    flash_message 'There are no results for this search. Please try 

```

again.’  
redirect\_to :action =\> ‘index’  
else  
@list\_pages, @list\_objects = paginate :homes, :per\_page =\> 25,  
:conditions =\> condition.to\_sql  
render :action =\> ‘list’  
end  
else  
render :action =\> ‘index’  
end  
end

Everything in the table is returned because the SQL used is SELECT \*  
FROM homes WHERE (postcode LIKE ‘%%’). Here is the development log  
output:-

Processing HomeController#search (for 127.0.0.1 at 2006-01-31 16:29:07)  
[POST]  
Parameters: {“home\_search”=\>{“postcode”=\>“G77”, “room\_types”=\>“”,  
“care\_types”=\>“nursing care”}, “commit”=\>“Search”, “action”=\>“search”,  
“controller”=\>“home”, “max\_price”=\>“2000”, “min\_price”=\>“0”}  
e[4;36;1mHome Count (0.002739)e[0m e[0;1mSELECT COUNT(_) FROM homes  
WHERE (postcode LIKE ‘%%’) e[0m  
e[4;35;1mHome Count (0.002695)e[0m e[0mSELECT COUNT(_) FROM homes  
WHERE  
(postcode LIKE ‘%%’) e[0m  
e[4;36;1mHome Load (0.003985)e[0m e[0;1mSELECT \* FROM homes WHERE  
(postcode LIKE ‘%%’) LIMIT 0, 25e[0m  
Rendering layoutfalseactionlist within layouts/application Rendering  
home/list Rendered shared/\_list\_left\_side (0.00054)  
e[4;35;1mHome Columns (0.001351)e[0m e[0mSHOW FIELDS FROM homese[0m  
Rendered shared/\_list\_object (0.04215)  
Rendered shared/\_list\_object (0.00287)  
Rendered shared/\_list\_object (0.00250)  
Rendered shared/\_list\_object (0.00266)  
Rendered shared/\_list\_object (0.00250)  
Rendered shared/\_list\_object (0.00267)  
Rendered shared/\_list\_object (0.00823)  
Rendered shared/\_list\_object (0.00245)  
Rendered shared/\_list\_object (0.00292)  
Rendered shared/\_list\_object (0.00254)  
Rendered shared/\_list\_object (0.00229)  
Rendered shared/\_list\_object (0.00230)  
Rendered shared/\_list\_object (0.00248)  
Rendered shared/\_list\_object (0.00238)  
Rendered shared/\_list\_object (0.00232)  
Rendered shared/\_list\_object (0.00248)  
Rendered shared/\_list\_object (0.00256)  
Rendered shared/\_list\_object (0.00231)  
Rendered shared/\_list\_object (0.00247)  
Rendered shared/\_list\_object (0.00245)  
Rendered shared/\_list\_object (0.00232)  
Rendered shared/\_list\_object (0.00230)  
Rendered shared/\_list\_object (0.00255)  
Rendered shared/\_list\_object (0.00232)  
Rendered shared/\_list\_object (0.00232)  
Rendered shared/\_list\_right\_side (0.13730) Rendered shared/\_html\_head  
(0.00587) Rendered shared/\_top\_menu (0.00289) Rendered  
shared/\_agecare\_menu (0.00548) Rendered shared/\_footer (0.00178)  
Completed in 0.20923 (4 reqs/sec) | Rendering: 0.16196 (77%) | DB:  
0.01077  
(5%) | 200 OK [[http://localhost/home/search](http://localhost/home/search)]

I know I am doing something really stupid but I have been going round in  
circles all afternoon. I would really appreciate any assistance.  
Regards  
Harvey

On 31/1/06 03:32, “Ezra Z.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Friends-
> 
> I wanted to let people know that there is a new experimental release  
> of this plugin. I would love feedback on syntax and features. There is  
> now a full test suite with fixtures that covers all the available  
> syntax. Look at the test suite for more syntax possibilities. There  
> have been many additions since my last release. Fabien Atelier has  
> been working on this with me and has added a bunch of great code.
> 
> You can get it from here:
> 
> [http://http](http://http)://brainspl.at/opensvn.csie.org/ezra/rails/plugins/dev/ez\_w  
> here/
> 
> This is a request for feedback. I want to solidify the api soon so  
> suggestions are welcome.
> 
> Here’s a snippet from the README:
> 
> Welcome to the new improved ez\_where plugin for rails. This plugin is  
> meant to be used as a nice ruby like syntax for creating the  
> :conditions part of an ActiveRecord::Base.find. We also add the ActiveRecord::Base.ez\_find method.  
> This method takes a block to simplify single and multi table queries.  
> There is now support for nested sub conditions using a few different  
> techniques. The default boolean used is ‘AND’. any takes a subcondition block and uses ‘OR’  
> to join them together.
> 
> @articles = Article.ez\_find(:all, :include =\> :author) do |article, author|  
> article.title =~ “%Foo Title%”  
> author.any do  
> name == ‘Ezra’  
> name == ‘Fab’  
> end  
> end
> 
> This issue a find with these :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.
> 
> And here is a link to my blog with the entire README for your perusal:
> 
> [Ruby on Rails Blog / What is Ruby on Rails for?](http://brainspl.at/articles/2006/01/30/i-have-been-busy)
> 
> Cheers-
> 
> -Ezra Z.  
> WebMaster  
> Yakima Herald-Republic Newspaper  
> removed\_email\_address@domain.invalid  
> 509-577-7732
> 
> This e-mail has been scanned for all viruses by MessageLabs.
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Ezra Z. wrote:

> Friends-
> 
> I wanted to let people know that there is a new experimental release  
> of this plugin. I would love feedback on syntax and features. There  
> is now a full test suite with fixtures that covers all the available  
> syntax. Look at the test suite for more syntax possibilities. There  
> have been many additions since my last release. Fabien Atelier has  
> been working on this with me and has added a bunch of great code.
> 
> You can get it from here:
> 
> [http://http](http://http)://brainspl.at/opensvn.csie.org/ezra/rails/plugins/dev/  
> ez\_where/
> 
> This is a request for feedback. I want to solidify the api soon so  
> suggestions are welcome.
> 
> Here’s a snippet from the README:
> 
> Welcome to the new improved ez\_where plugin for rails. This plugin is  
> meant  
> to be used as a nice ruby like syntax for creating the :conditions  
> part of an  
> ActiveRecord::Base.find. We also add the ActiveRecord::Base.ez\_find  
> method.  
> This method takes a block to simplify single and multi table queries.  
> There  
> is now support for nested sub conditions using a few different  
> techniques. The  
> default boolean used is ‘AND’. any takes a subcondition block and  
> uses ‘OR’  
> to join them together.
> 
> @articles = Article.ez\_find(:all, :include =\> :author) do |article,  
> author|  
> article.title =~ “%Foo Title%”  
> author.any do  
> name == ‘Ezra’  
> name == ‘Fab’  
> end  
> end
> 
> This issue a find with these :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.
> 
> And here is a link to my blog with the entire README for your perusal:
> 
> [Ruby on Rails Blog / What is Ruby on Rails for?](http://brainspl.at/articles/2006/01/30/i-have-been-busy)
> 
> Cheers-  
> -Ezra Z.  
> WebMaster  
> Yakima Herald-Republic Newspaper  
> removed\_email\_address@domain.invalid  
> 509-577-7732
