# How to do a find with conditions that contain an OR

**URL:** <https://www.ruby-forum.com/t/how-to-do-a-find-with-conditions-that-contain-an-or/53596>\
**Category:** Rails\
**Created:** [February 11, 2006, 12:00am UTC](https://www.ruby-forum.com/t/how-to-do-a-find-with-conditions-that-contain-an-or/53596 "2006-02-11T00:00:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiheng\_xu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/0ea827/32.png) [@xiheng\_xu](https://www.ruby-forum.com/u/xiheng_xu)\
**Post date:** [February 11, 2006, 12:00am UTC](https://www.ruby-forum.com/t/how-to-do-a-find-with-conditions-that-contain-an-or/53596/1 "2006-02-11T00:00:25Z")

</div>

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 -

---

<div class="post-metadata">

**Author:** ![xiheng\_xu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/0ea827/32.png) [@xiheng\_xu](https://www.ruby-forum.com/u/xiheng_xu)\
**Post date:** [February 11, 2006, 12:12am UTC](https://www.ruby-forum.com/t/how-to-do-a-find-with-conditions-that-contain-an-or/53596/2 "2006-02-11T00:12:54Z")

</div>

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](http://www.ericgoodwin.com)

---

<div class="post-metadata">

**Author:** ![xiheng\_xu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/0ea827/32.png) [@xiheng\_xu](https://www.ruby-forum.com/u/xiheng_xu)\
**Post date:** [February 11, 2006, 3:57am UTC](https://www.ruby-forum.com/t/how-to-do-a-find-with-conditions-that-contain-an-or/53596/3 "2006-02-11T03:57:51Z")

</div>

On 2/10/06, Eric G. [removed\_email\_address@domain.invalid](mailto: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])

---

<div class="post-metadata">

**Author:** ![xiheng\_xu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/0ea827/32.png) [@xiheng\_xu](https://www.ruby-forum.com/u/xiheng_xu)\
**Post date:** [February 13, 2006, 4:31pm UTC](https://www.ruby-forum.com/t/how-to-do-a-find-with-conditions-that-contain-an-or/53596/4 "2006-02-13T16:31:02Z")

</div>

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](http://lists.rubyonrails.org/mailman/listinfo/rails)

- RoR newbie -

---

<div class="post-metadata">

**Author:** ![xiheng\_xu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/0ea827/32.png) [@xiheng\_xu](https://www.ruby-forum.com/u/xiheng_xu)\
**Post date:** [February 13, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/how-to-do-a-find-with-conditions-that-contain-an-or/53596/5 "2006-02-13T16:33:00Z")

</div>

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](http://lists.rubyonrails.org/mailman/listinfo/rails)

- RoR newbie -
