Ez_where query question


#1

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
Sent from the RubyOnRails Users forum at Nabble.com.


#2

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

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


#3

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


#4

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
Sent from the RubyOnRails Users forum at Nabble.com.