Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding records created 10 days ago

My initial thought was:

user.humans.where("created_at = ?", 10.days.ago)

Though, this seems to be looking for the record created 10 days ago at the exact time when the statement is called. I want to collect the records created on that day, regardless of their time.

Is anyone aware of a convenient way to do this? Let me know if I need to elaborate.

Thanks.

like image 854
John Long Avatar asked Apr 20 '15 15:04

John Long


1 Answers

You'll probably want to use a range here, as I assume this is a datetime column.

User.humans.where("? <= created_at AND created_at <= ?", 10.days.ago.beginning_of_day, 10.days.ago.end_of_day)

You'll also want to make sure you're setting the time zone of your Rails application so that you're explicit about which time period you consider to be the 10th day.

like image 167
joshhepworth Avatar answered Sep 22 '22 06:09

joshhepworth