Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing a Date to the Datetime Created_at in rails3

So I'm trying to do things like so:

today = Date.today - 1
todaysReport = Report.where(:created_at => today).find_by_user_id(user.id)

The problem is created_at is a datetime, so it won't find any matches..Any suggestions?

like image 968
Elliot Avatar asked Jul 03 '11 00:07

Elliot


1 Answers

You probably want something like this:

yesterday = Time.now - 1.day
user = User.find(user_id)

todaysReport = user.reports.where(["created_at >= ? AND created_at <= ?", yesterday.beginning_of_day, yesterday.end_of_day])
like image 126
Dex Avatar answered Nov 02 '22 19:11

Dex