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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 3/19/08, Tom Ha <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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'']) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 19, 2008, at 3:38 PM, Adam Cohen wrote:> On 3/19/08, Tom Ha <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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''])...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 Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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''] }) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---