我很难用这个来挠头。我有一个任务课。任务可以在一周中的每一天重复。我有一个分配任务的处理程序类。我目前正在处理仪表板,以根据一周中的哪一天向处理程序显示他们今天有哪些任务。
在我的处理程序模型中:
def todays_walks
# Sunday
if Time.now.wday == 0
Task.where('handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_sunday = ?', id, Time.now.beginning_of_day, true)
end
# Monday
if Time.now.wday == 1
Task.where('handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_monday = ?', id, Time.now.beginning_of_day, true)
end
# Tuesday
if Time.now.wday == 2
Task.where('handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_tuesday = ?', id, Time.now.beginning_of_day, true)
end
# Wednesday
if Time.now.wday == 3
Task.where('handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_wednesday = ?', id, Time.now.beginning_of_day, true)
end
# Thursday
if Time.now.wday == 4
Task.where('handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_thursday = ?', id, Time.now.beginning_of_day, true)
end
# Friday
if Time.now.wday == 5
Task.where('handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_friday = ?', id, Time.now.beginning_of_day, true)
end
# Saturday
if Time.now.wday == 6
Task.where('handler_id = ? AND start_time >= ? AND tasktype_id = 1 AND repeat_saturday = ?', id, Time.now.beginning_of_day, true)
end
end
然后在我看来:
<h2>Your Day</h2>
<h3>Pups to walk:</h3>
<% if @handler.todays_walks.empty? %>
<p>You have no dogs to walk today.</p>
<% else %>
<%= render :partial => 'admin_shared/dashboard_pup', :collection => @handler.todays_walks.pup, :as => :pup %>
<% end %>
如果我强制除星期日之外的一周中的任何一天,它都会起作用。我想这在某种程度上是因为周日评估为 0?我想不通。