Converting times for db query

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

Mr Bear -

On 28-Nov-07, at 5:04 PM, blinking bear wrote:

@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

Haha - I new it! It’s been too long… thanks

That works as well - thank you.

On Nov 28, 2007, at 5:12 PM, Jodi S. wrote:

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 B. http://agileconsultingllc.com
[email protected]