# Query for multiple possible values of a field

**URL:** <https://www.ruby-forum.com/t/query-for-multiple-possible-values-of-a-field/160908>\
**Category:** Rails\
**Created:** [February 20, 2009, 8:58pm UTC](https://www.ruby-forum.com/t/query-for-multiple-possible-values-of-a-field/160908 "2009-02-20T20:58:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![naijaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9e8a1a/32.png) [@naijaguy](https://www.ruby-forum.com/u/naijaguy)\
**Post date:** [February 20, 2009, 8:58pm UTC](https://www.ruby-forum.com/t/query-for-multiple-possible-values-of-a-field/160908/1 "2009-02-20T20:58:08Z")

</div>

[http://geokit.rubyforge.org/readme.html](http://geokit.rubyforge.org/readme.html)

While browsing the above page, I looked at this query and wondered if it  
could be changed to include multiple possible values for state:

find(:all, :origin =\> @somewhere, :within =\> 5,  
:conditions=\>[‘state=?’,state])

How would I tell ActiveRecord to return any records where state was ‘CA’  
or ‘TX’ or ‘NY’? It seems like it’d be more efficient to go ahead and  
constrain the results from the beginning…

Thanks!

---

<div class="post-metadata">

**Author:** ![naijaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9e8a1a/32.png) [@naijaguy](https://www.ruby-forum.com/u/naijaguy)\
**Post date:** [February 20, 2009, 9:01pm UTC](https://www.ruby-forum.com/t/query-for-multiple-possible-values-of-a-field/160908/2 "2009-02-20T21:01:01Z")

</div>

states = [‘CA’, ‘TX’, ‘LA’]  
find(:all, :origin =\> @somewhere, :within =\> 5, :conditions=\>[‘state  
IN ( ? )’,states])

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

On Fri, Feb 20, 2009 at 4:58 PM, Naija G.

---

<div class="post-metadata">

**Author:** ![naijaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9e8a1a/32.png) [@naijaguy](https://www.ruby-forum.com/u/naijaguy)\
**Post date:** [February 22, 2009, 1:12am UTC](https://www.ruby-forum.com/t/query-for-multiple-possible-values-of-a-field/160908/3 "2009-02-22T01:12:33Z")

</div>

Thanks a lot! What if my attribute has multiple values? For example,  
if a user is associated with multiple states and the model User has an  
attribute states that’s a collection of strings how would I check that  
against the query you showed me? So user.states returns NY and MA, and  
I’m trying to see if the given user’s states is in the applicable states  
for the rule?

I need to change the query to something like

user.states in states

…but it seems like I’ll have to modify yours quite a bit more!

MaurÃ­cio Linhares wrote:

> states = [‘CA’, ‘TX’, ‘LA’]  
> find(:all, :origin =\> @somewhere, :within =\> 5, :conditions=\>[‘state  
> IN ( ? )’,states])
> 
> - 
> 
> Maurï¿½cio Linhares  
> [http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
> (en)
> 
> On Fri, Feb 20, 2009 at 4:58 PM, Naija G.
