How to do a find with conditions that contain an OR


#1

Hi, I’m trying to do a find with a condition that has an OR on the same
column. Say I only want to use find() but not find_by_sql(). I reduced
my problems into the following:

values = []
… # filling up values from params
Order.find(:all,
:conditions => ["(action = ? OR action = ?) AND price = ?",
values]

For example I wanted the condition to be
“(action = ‘SELL’ OR action = ‘SHORT’) AND price = ‘10.00’”

Here I can’t use as a hash because the keys for the two
values would be the same. I tried to use the variable values
as an array, then as a comma-separated string. Both gave me the
following error:
wrong number of bind variables (1 for 3) in:
(action = ? OR action = ?) AND price = ?

I didn’t find any answers from the Agile book. Can anyone help? Thanks
very much!

  • RoR newbie -

#2

Hi,
You have to have them seperate.
ie.
Order.find(:all,:conditions => ["(action = ? OR action = ?) AND price =
?", action, action, price])

Eric

xiheng xu wrote:

For example I wanted the condition to be
very much!

  • RoR newbie -


Eric G.
http://www.ericgoodwin.com


#3

On 2/10/06, Eric G. removed_email_address@domain.invalid wrote:

Hi,
You have to have them seperate.
ie.
Order.find(:all,:conditions => ["(action = ? OR action = ?) AND price = ?", action, action, price])

Not sure if this works, but you could try:

Order.find(:all,:conditions => [“action IN (?) AND price = ?”, actions,
price])


#4

Thanks guys!

No Jeremy, that didn’t work for me… I came to understand my question
is really not about using an OR in the find query.

The problem is that I actually construct the query as well as the
parameter values from users input before i put them into the find().
This comes from the fact that I have a big form with almost every text
field being optional for the ultimate selection in the legacy table.

I think I can use the unsafe approach of embedding the values into the
doublequotes. But that’s not recommended, is it? The Agile book on
p.206 says “This condition can be either a string containing SQL or an
array containing SQL and substitution values”. I took it literally and
that’s why I pumped both the query and the arguments into one array on
the RHS of the :conditions =>…

Is there solutions other than having everything within double quotes?

Thanks to all!
-xiheng

On Friday, February 10, 2006, at 6:57 PM, Jeremy E. wrote:

actions, price])


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

  • RoR newbie -

#5

Thanks guys!

No Jeremy, that didn’t work for me… I came to understand my question
is really not about using an OR in the find query.

The problem is that I actually construct the query as well as the
parameter values from users input before i put them into the find().
This comes from the fact that I have a big form with almost every text
field being optional for the ultimate selection in the legacy table.

I think I can use the unsafe approach of embedding the values into the
doublequotes. But that’s not recommended, is it? The Agile book on
p.206 says “This condition can be either a string containing SQL or an
array containing SQL and substitution values”. I took it literally and
that’s why I pumped both the query and the arguments into one array on
the RHS of the :conditions =>…

Is there a solution other than having everything within double quotes?

Thanks to all!
-xiheng

On Friday, February 10, 2006, at 6:57 PM, Jeremy E. wrote:

actions, price])


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

  • RoR newbie -