# SQL OR in Rails

**URL:** <https://www.ruby-forum.com/t/sql-or-in-rails/151434>\
**Category:** Rails\
**Created:** [October 21, 2008, 12:52pm UTC](https://www.ruby-forum.com/t/sql-or-in-rails/151434 "2008-10-21T12:52:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Schalk\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@Schalk\_N](https://www.ruby-forum.com/u/Schalk_N)\
**Post date:** [October 21, 2008, 12:52pm UTC](https://www.ruby-forum.com/t/sql-or-in-rails/151434/1 "2008-10-21T12:52:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Schalk\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@Schalk\_N](https://www.ruby-forum.com/u/Schalk_N)\
**Post date:** [October 21, 2008, 1:02pm UTC](https://www.ruby-forum.com/t/sql-or-in-rails/151434/2 "2008-10-21T13:02:00Z")

</div>

Hi Schalk

Just use:

@states = States.find(:all, :conditions =\> [“country=? OR  
country=‘International’”, LOCALE])

Jan

---

<div class="post-metadata">

**Author:** ![Schalk\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@Schalk\_N](https://www.ruby-forum.com/u/Schalk_N)\
**Post date:** [October 21, 2008, 1:36pm UTC](https://www.ruby-forum.com/t/sql-or-in-rails/151434/3 "2008-10-21T13:36:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Schalk\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@Schalk\_N](https://www.ruby-forum.com/u/Schalk_N)\
**Post date:** [October 21, 2008, 2:13pm UTC](https://www.ruby-forum.com/t/sql-or-in-rails/151434/4 "2008-10-21T14:13:08Z")

</div>

Thanks Jan.

---

<div class="post-metadata">

**Author:** ![Schalk\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@Schalk\_N](https://www.ruby-forum.com/u/Schalk_N)\
**Post date:** [October 21, 2008, 1:44pm UTC](https://www.ruby-forum.com/t/sql-or-in-rails/151434/5 "2008-10-21T13:44:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Schalk\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@Schalk\_N](https://www.ruby-forum.com/u/Schalk_N)\
**Post date:** [October 21, 2008, 2:34pm UTC](https://www.ruby-forum.com/t/sql-or-in-rails/151434/6 "2008-10-21T14:34:52Z")

</div>

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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Schalk\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@Schalk\_N](https://www.ruby-forum.com/u/Schalk_N)\
**Post date:** [October 21, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/sql-or-in-rails/151434/7 "2008-10-21T17:15:14Z")

</div>

I believe you can also do:

@states = States.find\_by\_country([LOCALE, ‘International’])

Worth a try…

Cheers,

-Roy
