0

我很难用这个来挠头。我有一个任务课。任务可以在一周中的每一天重复。我有一个分配任务的处理程序类。我目前正在处理仪表板,以根据一周中的哪一天向处理程序显示他们今天有哪些任务。

在我的处理程序模型中:

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?我想不通。

4

2 回答 2

1

我不确定为什么这对您不起作用,但我将从快速重构开始,以使此方法更加干燥。尝试这个:

def todays_walks
  days = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"]
  return Task.where("handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_#{days[Time.now.wday]} = ?", id, Time.now.beginning_of_day, true)
end
于 2013-10-20T19:37:25.610 回答
0

我想通了......只是一个'duh'的时刻。该模型实际上并没有返回对象。它在其他日子有效,因为他们有重复的任务。

所以要修复,在模型中:

if Time.now.wday == 0
  return Task.where('handler_id = ? AND start_time <= ? AND tasktype_id = 1 AND repeat_sunday = ?', id, Time.now.beginning_of_day, true)
end
于 2013-10-20T19:27:56.883 回答