# Limiting query with created\_at?

**URL:** <https://www.ruby-forum.com/t/limiting-query-with-created-at/88216>\
**Category:** Rails\
**Created:** [March 8, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/limiting-query-with-created-at/88216 "2007-03-08T23:37:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 8, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/limiting-query-with-created-at/88216/1 "2007-03-08T23:37:06Z")

</div>

I have no idea why this isn’t working but maybe a new set of eyes can  
help. I want to only collect posts from yesterday:

```
Post.find( :all,
           :conditions => "posts.created_at > #{(1.day.ago).to_i}",
           :order => "posts.created_at DESC",
           :include => :person)

```

This returns all posts. Dev log shows:

SELECT posts.`id` AS t0\_r0, \<\> FROM posts LEFT OUTER  
JOIN people ON people.id = posts.person\_id WHERE (is\_deleted = false AND  
posts.created\_at \> 1173306785) ORDER BY posts.created\_at DESCe

Even 1.minute.ago returns all posts. If flipping \> with \< returns 0  
results. Grrr. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 8, 2007, 11:44pm UTC](https://www.ruby-forum.com/t/limiting-query-with-created-at/88216/2 "2007-03-08T23:44:27Z")

</div>

Taylor S. wrote:

> I have no idea why this isn’t working but maybe a new set of eyes can  
> help. I want to only collect posts from yesterday:
> 
> ```
> Post.find( :all,
> :conditions => "posts.created_at > #{(1.day.ago).to_i}",
> 
> ```

I though all databases only treated dates as formatted strings. Try  
this:

```
            :conditions => "posts.created_at > '#{1.day.ago.to_s 

```

:db}'",

After that guess, I don’t know how a database will handle date  
inequalities!

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 9, 2007, 12:04am UTC](https://www.ruby-forum.com/t/limiting-query-with-created-at/88216/3 "2007-03-09T00:04:28Z")

</div>

> I have no idea why this isn’t working but maybe a new set of eyes can  
> help. I want to only collect posts from yesterday:
> 
> Post.find( :all,  
> :conditions =\> “posts.created\_at \> #{(1.day.ago).to\_i}”,  
> :order =\> “posts.created\_at DESC”,  
> :include =\> :person)

Let Rails convert the time into the format your database expects…

:conditions =\> [“posts.created\_at \> ?”, 1.day.ago]

Otherwise you’re going to need to format that time so your database  
understands it.

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 9, 2007, 1:44am UTC](https://www.ruby-forum.com/t/limiting-query-with-created-at/88216/4 "2007-03-09T01:44:34Z")

</div>

> Let Rails convert the time into the format your database expects…
> 
> :conditions =\> [“posts.created\_at \> ?”, 1.day.ago]
> 
> Otherwise you’re going to need to format that time so your database  
> understands it.

That worked great. Thanks!
