Condition using "OR"

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

On 3/19/08, Tom Ha [email protected] 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’])

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
[email protected]

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’] })