Rails ActiveRecord查询日期范围 [英] 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)
我丢失了什么?
推荐答案
我的控制器以某种方式设置不正确,并且不再保存 current_user
的ID在创建新任务时,这就是为什么找不到它们的原因。我使用rails控制台并在提交的最后几个作业中查找了答案。 user_id
设置为 nil
。感谢您的协助,@ mu太短了。
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.
这篇关于Rails ActiveRecord查询日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!