# Condition using "OR"

**URL:** <https://www.ruby-forum.com/t/condition-using-or/132405>\
**Category:** Rails\
**Created:** [March 19, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/condition-using-or/132405 "2008-03-19T19:59:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 19, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/condition-using-or/132405/1 "2008-03-19T19:59:30Z")

</div>

Hi there,

how do I correctly write a condition using an “OR” operator (if I don’t  
want to use SQL).

I’d need something like this (see the 3rd condition):

:conditions =\> { condition1, condition2, :status =\> ‘sent’ OR ‘read’ }

Thanks for your help!  
Tom

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 19, 2008, 8:39pm UTC](https://www.ruby-forum.com/t/condition-using-or/132405/2 "2008-03-19T20:39:19Z")

</div>

On 3/19/08, Tom Ha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi there,
> 
> how do I correctly write a condition using an “OR” operator (if I don’t  
> want to use SQL).
> 
> I’d need something like this (see the 3rd condition):
> 
> :conditions =\> { condition1, condition2, :status =\> ‘sent’ OR ‘read’ }

my\_model.find(:all, :conditions =\> [‘status = ? or status = ?’, ‘sent’,  
‘read’])

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 19, 2008, 9:43pm UTC](https://www.ruby-forum.com/t/condition-using-or/132405/3 "2008-03-19T21:43:44Z")

</div>

On Mar 19, 2008, at 3:38 PM, Adam C. wrote:

> :conditions =\> { condition1, condition2, :status =\> ‘sent’ OR ‘read’ }
> 
> my\_model.find(:all, :conditions =\> [‘status = ? or status = ?’,  
> ‘sent’, ‘read’])

…but if you have other conditions, you’ll have to do something like:

my\_model.find(:all,  
:conditions =\> [‘col1 = cond1 AND col2 = cond2 AND (status = ? or  
status = ?)’,  
‘sent’, ‘read’])

Note the ( ) around the status clauses. You can also use ‘IN’

my\_model.find(:all,  
:conditions =\> [‘col1 = cond1 AND col2 = cond2 AND status IN (?)’,  
[‘sent’, ‘read’]])

if you put the set of values that status can match into an Array (and  
then you don’t need to have extra ()'s around the status clauses).  
Now if this is still too much SQL for you, you can try (because I  
_haven’t_) something like:

my\_model.find(:all,  
:conditions =\> { :col1 =\> cond1, :col2 =\> cond2, :status =\> [‘sent’,  
‘read’] })

(Again, this is an untested idea that I didn’t even bounce off the  
rdocs)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 19, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/condition-using-or/132405/4 "2008-03-19T22:17:21Z")

</div>

Thanks a lot, guys!

Rob - your last version definitely works and it’s the best one (it’s the  
most programmer friendly & rails style):

my\_model.find(:all, :conditions =\> { :col1 =\> cond1,  
:col2 =\> cond2,  
:status =\> [‘sent’, ‘read’] })
