Forum: Ruby on Rails SQL OR in Rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
44391f94143d129bdfdcd8b52aa51674?d=identicon&s=25 Schalk Neethling (Guest)
on 2008-10-21 12:52
(Received via mailing list)
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
Ae2e636871f0382a09781ffdc5c014d9?d=identicon&s=25 javinto (Guest)
on 2008-10-21 13:02
(Received via mailing list)
Hi Schalk

Just use:

@states = States.find(:all, :conditions => ["country=? OR
country='International'", LOCALE])

Jan
44391f94143d129bdfdcd8b52aa51674?d=identicon&s=25 Schalk Neethling (Guest)
on 2008-10-21 13:36
(Received via mailing list)
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
Ae2e636871f0382a09781ffdc5c014d9?d=identicon&s=25 javinto (Guest)
on 2008-10-21 13:44
(Received via mailing list)
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
44391f94143d129bdfdcd8b52aa51674?d=identicon&s=25 Schalk Neethling (Guest)
on 2008-10-21 14:13
(Received via mailing list)
Thanks Jan.
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-10-21 14:34
(Received via mailing list)
On Oct 21, 2008, at 7:43 AM, javinto wrote:
> The second <?> with the 3rd element
> And so on
>
> Jan

You 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@AgileConsultingLLC.com
D6434aa0b7b350f8c3ed0119d81b2ead?d=identicon&s=25 Roy Pardee (rpardee)
on 2008-10-21 17:15
(Received via mailing list)
I believe you can also do:

  @states = States.find_by_country([LOCALE, 'International'])

Worth a try...

Cheers,

-Roy
This topic is locked and can not be replied to.