Picking up a rails project for the first time in a few months and drawing a blank here... I want to compare a created_at datetime field in a find() call @users_week = User.count( :conditions => "created_at > ''#{7.days.ago}''") MySQL doesn''t like the format of that date so I added this: class Time def to_db self.strftime("%Y-%m-%d %H:%M:%S.0") end end and then: @users_week = User.count( :conditions => "created_at > ''#{7.days.ago.*to_db* }''") I''m sure there is a ''dead simple'' way to do this thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mr Bear - On 28-Nov-07, at 5:04 PM, blinking bear wrote:> MySQL doesn''t like the format of that date so I added this: > > class Time > def to_db > self.strftime("%Y-%m-%d %H:%M:%S.0") > end > end > > and then: > > @users_week = User.count( :conditions => "created_at > ''# > {7.days.ago.to_db}''") >methinks 7.days.ago.to_s(:db) will get you there. cheers, little*bear --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Haha - I new it! It''s been too long... thanks On Nov 28, 2007 5:12 PM, Jodi Showers <jodi-BOB1p6JRLoAV+D8aMU/kSg@public.gmane.org> wrote:> Mr Bear - > On 28-Nov-07, at 5:04 PM, blinking bear wrote: > > MySQL doesn''t like the format of that date so I added this: > > class Time > def to_db > self.strftime("%Y-%m-%d %H:%M:%S.0") > end > end > > and then: > > @users_week = User.count( :conditions => "created_at > ''#{7.days.ago.* > to_db*}''") > > > methinks 7.days.ago.to_s(:db) will get you there. > > cheers, little*bear > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Nov 28, 2007, at 5:12 PM, Jodi Showers wrote:> Mr Bear - > > On 28-Nov-07, at 5:04 PM, blinking bear wrote: >> MySQL doesn''t like the format of that date so I added this: >> >> class Time >> def to_db >> self.strftime("%Y-%m-%d %H:%M:%S.0") >> end >> end >> >> and then: >> >> @users_week = User.count( :conditions => "created_at > >> ''#{7.days.ago.to_db}''") >> > > methinks 7.days.ago.to_s(:db) will get you there. > > cheers, little*bearWell, if it''s "dead simple" that you want, why not: @users_week = User.count( :conditions => ["created_at > ?", 7.days.ago]) Then you let the database adapter worry about both the formatting (which Rails gives you as 7.days.ago.to_s(:db) anyway, no need to define your own) and the quoting. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
That works as well - thank you. On Nov 28, 2007 5:25 PM, Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> wrote:> On Nov 28, 2007, at 5:12 PM, Jodi Showers wrote: > > Mr Bear - > On 28-Nov-07, at 5:04 PM, blinking bear wrote: > > MySQL doesn''t like the format of that date so I added this: > > class Time > def to_db > self.strftime("%Y-%m-%d %H:%M:%S.0") > end > end > > and then: > > @users_week = User.count( :conditions => "created_at > ''#{7.days.ago.* > to_db*}''") > > > methinks 7.days.ago.to_s(:db) will get you there. > > cheers, little*bear > > > Well, if it''s "dead simple" that you want, why not: > > @users_week = User.count( :conditions => ["created_at > ?", 7.days.ago]) > > Then you let the database adapter worry about both the formatting (which > Rails gives you as 7.days.ago.to_s(:db) anyway, no need to define your > own) and the quoting. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---