Forum: Ruby on Rails Comparing Ruby time with SQL time

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.
Dave A. (Guest)
on 2007-03-01 04:59
Hi everyone,

Quick question: I want to grab all rows in a database posted less than
24 hours ago. Here's what I have in my controller:

def stats
  now = Time.now
  recipe_yesterday = now.yesterday
  @recipes = Recipe.find(:all, :conditions => "date_added >
#{recipe_yesterday}")
end

I think there's a problem because the formatting of the time generated
by Time is different than how time is stored in my database. Is there a
way to reconcile the two?

Thanks!

Dave
unknown (Guest)
on 2007-03-01 05:10
(Received via mailing list)
Hi --

On Thu, 1 Mar 2007, Dave A. wrote:

> #{recipe_yesterday}")
> end
>
> I think there's a problem because the formatting of the time generated
> by Time is different than how time is stored in my database. Is there a
> way to reconcile the two?

Try this:

   Recipe.find(:all,
               :conditions => "date_added >
'#{recipe_yesterday.to_s(:db)}'")

There may be a more concise way to do it that I'm not remembering, but
that should give you comparable formats.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying!  http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
Bill W. (Guest)
on 2007-03-01 05:24
(Received via mailing list)
Hi Dave,

Dave A. wrote:

> I want to grab all rows in a database posted less than
> 24 hours ago. Here's what I have in my controller:
>
> def stats
>  now = Time.now
>  recipe_yesterday = now.yesterday
>  @recipes = Recipe.find(:all, :conditions => "date_added >
> #{recipe_yesterday}")
> end

Try:
@recipes = Recipe.find(:all, :conditions => ["date_added > ?",
recipe_yesterday])

hth,
Bill
This topic is locked and can not be replied to.