# ActiveRecord: SQL IN operator

**URL:** <https://www.ruby-forum.com/t/activerecord-sql-in-operator/143431>\
**Category:** Rails\
**Created:** [July 18, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/activerecord-sql-in-operator/143431 "2008-07-18T13:57:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [July 18, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/activerecord-sql-in-operator/143431/1 "2008-07-18T13:57:02Z")

</div>

I want to use an array in the :conditions of an ActiveRecord Find so  
that a SQL IN statement is created. However, I also want to specify a  
greater than condition on a timestamp column too.  
From the Docs:  
An array may be used in the hash to use the SQL IN operator:  
Student.find(:all, :conditions =\> { :grade =\> [9,11,12] })

How do I modify this so that it also contains the timestamp condition?  
:condition =\> [‘created\_at \> ?’,time]

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [July 18, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/activerecord-sql-in-operator/143431/2 "2008-07-18T16:08:58Z")

</div>

Hey…  
I’ve been doing Rails for a while and have never adopted the hash syntax  
for  
conditions. Someone else might be able to help you with that, but I  
know  
this will work:

Student.find :all, :conditions = \>[“grade in (?) and created\_at \> ?”,  
[9,11,12], time]

On Fri, Jul 18, 2008 at 6:57 AM, Tim C. \<

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [July 18, 2008, 4:27pm UTC](https://www.ruby-forum.com/t/activerecord-sql-in-operator/143431/3 "2008-07-18T16:27:07Z")

</div>

On Jul 18, 2008, at 10:08 AM, Brian H. wrote:

> Hey…  
> I’ve been doing Rails for a while and have never adopted the hash  
> syntax for conditions. Someone else might be able to help you with  
> that, but I know this will work:
> 
> Student.find :all, :conditions = \>["grade in (?) and created\_at
> 
> > ?", [9,11,12], time]

Yes, that’s it. If you like the Hash form for conditions, you might  
like DataMapper where the same query would look like:

Student.all(:grade =\> [9,11,12], :created\_at.gt =\> time)

> **[ROM](https://rom-rb.org)**
>
> An open-source persistence and mapping toolkit for Ruby built for speed and simplicity.

-Rob

> How do I modify this so that it also contains the timestamp condition?  
> :condition =\> [‘created\_at \> ?’,time]

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
