Forum: Ruby on Rails limiting query with created_at?

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.
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (Guest)
on 2007-03-08 23:37
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, <<SNIP ATTRIBUTES>> 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 DESC

Even 1.minute.ago returns all posts.  If flipping > with < returns 0
results.  Grrr.  Thanks in advance!
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-03-08 23:44
(Received via mailing list)
Taylor Strait 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  <-- NOT a blog!!
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2007-03-09 00:04
(Received via mailing list)
> 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.
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (Guest)
on 2007-03-09 01:44
> 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!
This topic is locked and can not be replied to.