Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails query by datetime range ( last 24, 48, etc... hours )

I'm trying to build a query that will search for recent entries based on column 'last_login_at'. This is a datetime field with time zone (i.e. Time.zone.now) When I execute

User.find(:all, :conditions => ["last_login_at < ?", 24.hours.ago])

I get nothing.

Alternatively I can define today as Time.zone.today and yesterday as Time.zone.today - 1.day and run

User.find(:all, :conditions => ["last_login_at between ? and ?", today, yesterday])

and it still returns 0 results. I know there are some entries that fall into this category.

like image 967
Michael Avatar asked Jan 28 '10 23:01

Michael


1 Answers

Old question, but here's newer syntax:

User.where(last_login_at: 3.days.ago..DateTime.now)
like image 166
AJcodez Avatar answered Nov 03 '22 19:11

AJcodez