Hi there everyone, How does one set-up a SQL OR query in Rails. I have the following SQL query: select state, state_abbr, country from states where country = ''Country'' or country = ''International'' order by state asc; Now in Rails: @states = States.find(:all, :conditions => [:country => LOCALE, :country => ''International'']) But the above will do a SQL AND query. What is the right keyword to use between those two conditions to turn this into a OR query? Thanks! Schalk --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Schalk Just use: @states = States.find(:all, :conditions => ["country=? OR country=''International''", LOCALE]) Jan On 21 okt, 12:50, Schalk Neethling <volume4.sch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi there everyone, > > How does one set-up a SQL OR query in Rails. > > I have the following SQL query: > > select state, state_abbr, country from states where country = ''Country'' > or country = ''International'' order by state asc; > > Now in Rails: > > @states = States.find(:all, :conditions => [:country => LOCALE, :country > => ''International'']) > > But the above will do a SQL AND query. What is the right keyword to use > between those two conditions to turn this into a OR query? > > Thanks! > Schalk--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Cool, thanks. My IDE was moaning when I did something similar to that. One should not always trust the IDE I guess, or does it have something to do with the fact that you moved the constant to the end? Kind Regards, Schalk javinto wrote:> Hi Schalk > > Just use: > > @states = States.find(:all, :conditions => ["country=? OR > country=''International''", LOCALE]) > > Jan > > > On 21 okt, 12:50, Schalk Neethling <volume4.sch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hi there everyone, >> >> How does one set-up a SQL OR query in Rails. >> >> I have the following SQL query: >> >> select state, state_abbr, country from states where country = ''Country'' >> or country = ''International'' order by state asc; >> >> Now in Rails: >> >> @states = States.find(:all, :conditions => [:country => LOCALE, :country >> => ''International'']) >> >> But the above will do a SQL AND query. What is the right keyword to use >> between those two conditions to turn this into a OR query? >> >> Thanks! >> Schalk > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi, I''m not sure how your code was, but you can do this as well: @states = States.find(:all, :conditions => ["country=? OR country=?", LOCALE, ''International'']) It''s just an array where every question mark will get replaced with it''s successive array element after having being Escaped by Rails. So the first <?> gets replaced with the 2nd element The second <?> with the 3rd element And so on Jan On 21 okt, 13:35, Schalk Neethling <volume4.sch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Cool, thanks. My IDE was moaning when I did something similar to that. > One should not always trust the IDE I guess, or does it have something > to do with the fact that you moved the constant to the end? > > Kind Regards, > Schalk > > javinto wrote: > > Hi Schalk > > > Just use: > > > @states = States.find(:all, :conditions => ["country=? OR > > country=''International''", LOCALE]) > > > Jan > > > On 21 okt, 12:50, Schalk Neethling <volume4.sch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Hi there everyone, > > >> How does one set-up a SQL OR query in Rails. > > >> I have the following SQL query: > > >> select state, state_abbr, country from states where country = ''Country'' > >> or country = ''International'' order by state asc; > > >> Now in Rails: > > >> @states = States.find(:all, :conditions => [:country => LOCALE, :country > >> => ''International'']) > > >> But the above will do a SQL AND query. What is the right keyword to use > >> between those two conditions to turn this into a OR query? > > >> Thanks! > >> Schalk--~--~---------~--~----~------------~-------~--~----~ 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 Jan. javinto wrote:> Hi, > > I''m not sure how your code was, but you can do this as well: > > @states = States.find(:all, :conditions => ["country=? OR country=?", > LOCALE, ''International'']) > > It''s just an array where every question mark will get replaced with > it''s successive array element after having being Escaped by Rails. > So the first <?> gets replaced with the 2nd element > The second <?> with the 3rd element > And so on > > Jan > > > On 21 okt, 13:35, Schalk Neethling <volume4.sch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Cool, thanks. My IDE was moaning when I did something similar to that. >> One should not always trust the IDE I guess, or does it have something >> to do with the fact that you moved the constant to the end? >> >> Kind Regards, >> Schalk >> >> javinto wrote: >>> Hi Schalk >>> Just use: >>> @states = States.find(:all, :conditions => ["country=? OR >>> country=''International''", LOCALE]) >>> Jan >>> On 21 okt, 12:50, Schalk Neethling <volume4.sch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>> Hi there everyone, >>>> How does one set-up a SQL OR query in Rails. >>>> I have the following SQL query: >>>> select state, state_abbr, country from states where country = ''Country'' >>>> or country = ''International'' order by state asc; >>>> Now in Rails: >>>> @states = States.find(:all, :conditions => [:country => LOCALE, :country >>>> => ''International'']) >>>> But the above will do a SQL AND query. What is the right keyword to use >>>> between those two conditions to turn this into a OR query? >>>> Thanks! >>>> Schalk > > >--~--~---------~--~----~------------~-------~--~----~ 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 Oct 21, 2008, at 7:43 AM, javinto wrote:> Hi, > > I''m not sure how your code was, but you can do this as well: > > @states = States.find(:all, :conditions => ["country=? OR country=?", > LOCALE, ''International'']) > > It''s just an array where every question mark will get replaced with > it''s successive array element after having being Escaped by Rails. > So the first <?> gets replaced with the 2nd element > The second <?> with the 3rd element > And so on > > JanYou have a special case there -- one column with any of a set of values. For that, you can use the SQL ''IN'' operation like this: States.find(:all, :conditions => [''country IN (?)'', [LOCALE, ''International'']] ) If you had more, countries = [ ...all the ones you needed... ] States.find(:all, :conditions => [''country IN (?)'', countries] ) It will work with an array having just one element also. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> On 21 okt, 13:35, Schalk Neethling <volume4.sch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Cool, thanks. My IDE was moaning when I did something similar to >> that. >> One should not always trust the IDE I guess, or does it have >> something >> to do with the fact that you moved the constant to the end? >> >> Kind Regards, >> Schalk >> >> javinto wrote: >>> Hi Schalk >> >>> Just use: >> >>> @states = States.find(:all, :conditions => ["country=? OR >>> country=''International''", LOCALE]) >> >>> Jan >> >>> On 21 okt, 12:50, Schalk Neethling <volume4.sch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>> Hi there everyone, >> >>>> How does one set-up a SQL OR query in Rails. >> >>>> I have the following SQL query: >> >>>> select state, state_abbr, country from states where country = >>>> ''Country'' >>>> or country = ''International'' order by state asc; >> >>>> Now in Rails: >> >>>> @states = States.find(:all, :conditions => [:country => >>>> LOCALE, :country >>>> => ''International'']) >> >>>> But the above will do a SQL AND query. What is the right keyword >>>> to use >>>> between those two conditions to turn this into a OR query? >> >>>> Thanks! >>>> Schalk--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I believe you can also do: @states = States.find_by_country([LOCALE, ''International'']) Worth a try... Cheers, -Roy -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of Schalk Neethling Sent: Tuesday, October 21, 2008 5:12 AM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: SQL OR in Rails Thanks Jan. javinto wrote:> Hi, > > I''m not sure how your code was, but you can do this as well: > > @states = States.find(:all, :conditions => ["country=? OR country=?", > LOCALE, ''International'']) > > It''s just an array where every question mark will get replaced with > it''s successive array element after having being Escaped by Rails. > So the first <?> gets replaced with the 2nd element The second <?> > with the 3rd element And so on > > Jan > > > On 21 okt, 13:35, Schalk Neethling <volume4.sch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Cool, thanks. My IDE was moaning when I did something similar to that. >> One should not always trust the IDE I guess, or does it have >> something to do with the fact that you moved the constant to the end? >> >> Kind Regards, >> Schalk >> >> javinto wrote: >>> Hi Schalk >>> Just use: >>> @states = States.find(:all, :conditions => ["country=? OR >>> country=''International''", LOCALE]) Jan On 21 okt, 12:50, Schalk >>> Neethling <volume4.sch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>> Hi there everyone, >>>> How does one set-up a SQL OR query in Rails. >>>> I have the following SQL query: >>>> select state, state_abbr, country from states where country = ''Country'' >>>> or country = ''International'' order by state asc; Now in Rails: >>>> @states = States.find(:all, :conditions => [:country => LOCALE, >>>> :country => ''International'']) But the above will do a SQL AND >>>> query. What is the right keyword to use between those two >>>> conditions to turn this into a OR query? >>>> Thanks! >>>> Schalk > > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---