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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Mar-01 03:09 UTC
Re: Comparing Ruby time with SQL time
Hi -- On Thu, 1 Mar 2007, Dave A. wrote:> > 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?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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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}") > endTry: @recipes = Recipe.find(:all, :conditions => ["date_added > ?", recipe_yesterday]) hth, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---