# Ez\_where query question

**URL:** <https://www.ruby-forum.com/t/ez-where-query-question/58060>\
**Category:** Rails\
**Created:** [April 18, 2006, 2:36am UTC](https://www.ruby-forum.com/t/ez-where-query-question/58060 "2006-04-18T02:36:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [April 18, 2006, 2:36am UTC](https://www.ruby-forum.com/t/ez-where-query-question/58060/1 "2006-04-18T02:36:47Z")

</div>

I’m having fun composing queries with ez\_where, but am stuck on how to  
formulate the following using the ez syntax:

(begin\_date \>= ? OR end\_date \>= ?) AND (description LIKE ? OR name LIKE  
?)

There are two clauses, each containing OR operators (the ez ‘any’  
syntax)  
but both clauses must evaulate to true for a match.

Can anyone suggest the proper way to construct an ez condition that  
generates this SQL?

## Thanks

View this message in context:  
[http://www.nabble.com/ez\_where-query-question-t1465220.html#a3960280](http://www.nabble.com/ez_where-query-question-t1465220.html#a3960280)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [April 18, 2006, 5:06am UTC](https://www.ruby-forum.com/t/ez-where-query-question/58060/2 "2006-04-18T05:06:17Z")

</div>

On Apr 17, 2006, at 5:35 PM, s.ross wrote:

> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Hi~

```
You can easily get this to work. Something like this:

```

ez ez $ script/console  
Loading development environment.

> > cond = Caboose::EZ::Condition.new :my\_table do  
> > ?\> any { foo == ‘bar’; name == ‘rails’ }  
> > any { baz! == ‘buzz’; name! == ‘loob’ }  
> > end  
> > =\> #\<Caboose::EZ::Condition:0x28ff724 ]\>  
> > cond.to\_sql  
> > =\> [“(my\_table.foo = ? OR my\_table.name = ?) AND (my\_table.baz != ?  
> > OR my\_table.name != ?)”, “bar”, “rails”, “buzz”, “loob”]

```
So by default when you specify two any commands like this they will

```

use OR inside the nested parens but it will use AND to join the two  
parens groups.

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [April 18, 2006, 6:28am UTC](https://www.ruby-forum.com/t/ez-where-query-question/58060/3 "2006-04-18T06:28:24Z")

</div>

On Mon, 2006-04-17 at 17:35 -0700, s.ross wrote:

> I’m having fun composing queries with ez\_where, but am stuck on how to  
> formulate the following using the ez syntax:
> 
> (begin\_date \>= ? OR end\_date \>= ?) AND (description LIKE ? OR name LIKE ?)
> 
> There are two clauses, each containing OR operators (the ez ‘any’ syntax)  
> but both clauses must evaulate to true for a match.
> 
> Can anyone suggest the proper way to construct an ez condition that  
> generates this SQL?

* * *

here’s controller code where I use the ez\_where and ‘or’

def services\_log\_residential\_all  
cond = Caboose::EZ::Condition.new  
cond.append [“pltype = ‘PAH’”], :or  
cond.append [“pltype = ‘16 Hour’”], :or  
@facility = Facility.find(:all,  
:conditions =\> cond.to\_sql,  
:order =\> ‘name’)  
@facility\_array = ( @facility.class == Array ? @facility :  
[@facility] )  
end

I hope this helps

Craig

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [April 18, 2006, 6:34am UTC](https://www.ruby-forum.com/t/ez-where-query-question/58060/4 "2006-04-18T06:34:52Z")

</div>

What I came up with was:

```
  cond = Caboose::EZ::Condition.new :events do
    any {begin_date >= Date.today; begin_date >= Date.today}
  end

  unless @phrase.nil?
    cond1 = Caboose::EZ::Condition.new :events do #!! strange code

```

alert !!  
any {description =~ condition\_clause; location =~  
condition\_clause; state =~ condition\_clause}  
end  
cond \<\< cond1  
end  
conditions = cond.to\_sql

The reason for the strange code alert is that this line should be:

cond \<\< Caboose::EZ::Condition.new :events do

## but for some reason the append operator throws a parse error. Anyhow, the code works fine and I’m a happy camper. Thanks Ezra and Craig.

View this message in context:  
[http://www.nabble.com/ez\_where-query-question-t1465220.html#a3962110](http://www.nabble.com/ez_where-query-question-t1465220.html#a3962110)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).
