49

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?

0

3 Answers 3

98

Should be:

@due_this_week = current_user.tasks.where(due_date: 1.week.ago..Date.today)
Sign up to request clarification or add additional context in comments.

4 Comments

How does that match "all tasks due less than a week from the current date"?
OP made a mistake, sorry.
Sweet. That's all.
It's been a while since I up voted this bad boy, and it came in handy again today!
5

FYI you can also use one sided ranges.

For all calls with due date from 1 week ago

@due_this_week = current_user.tasks.where(due_date: 1.week.ago..)

or for all calls with due date up to today

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.