Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ActiveRecord Query Date Range

I'm trying to use the following line in my controller to capture all tasks due less than a week from the current date:

@due_this_week = current_user.tasks.where(due_date: Date.today..1.week.from_now) 

For some reason it's not finding any results even I know I have tasks due within four and six days. This is the only instance variable using a range query. I have another one that works fine to find overdue tasks:

@overdue = current_user.tasks.where("due_date <= ?", Date.today) 

What am I missing?

like image 404
Cody Barr Avatar asked Dec 06 '13 00:12

Cody Barr


2 Answers

Should be:

@due_this_week = current_user.tasks.where(due_date: 1.week.ago..Date.today) 
like image 162
NARKOZ Avatar answered Sep 22 '22 04:09

NARKOZ


Turns out my controller somehow wasn't set up correctly and was no longer saving the current_user's ID when creating new assignments, which is why they weren't being found. I figured this out using the rails console and running a find on the last few assignments submitted. The user_id was set to nil. Thanks for the assist @mu is too short.

like image 24
Cody Barr Avatar answered Sep 23 '22 04:09

Cody Barr