Comparing Ruby time with SQL time

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

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)

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