0

我有一个跟踪员工时间的应用程序。员工必须每 12 天至少休息 2 天...员工应输入带有布尔真值的记录离开。我试图简单地计算日期递减一天的记录,从布尔 day_off 开始或连续日期中断......并在给定日期结束。

这是我正在研究的帮手

  def consecutive_days_on(user, dutylog)
    # dutylog will be supplied via a loop

    last_day_off = user.dutylogs.where("entry_date < ?", dutylog.entry_date).where(day_off: true).last
    start_date = dutylog.entry_date

      if last_day_off.present?
        end_date = last_day_off.entry_date
        # if the user logged their days off
      else
        end_date = user.dutylogs.where("entry_date < ?", dutylog.entry_date).last.entry_date
        # as of now this just finds the last record...it needs to iterate and increment date to find a break in days on
        # to find break in consecutive dates...if user did not log days off
      end

      user.dutylogs.where("entry_date >= ? AND entry_date <= ?", start_date, end_date).count
      # count the records between the two dates...to find consecutive days on
  end
4

1 回答 1

0

我重构了我的助手......并且它有效。我决定依靠用户在他们的日志中输入休息日……而不是假设输入的休息日可以被视为休息日。

   def consecutive_days_on(user, dutylog)
     last_day_off = user.dutylogs.where(day_off: true).where("entry_date < ?", dutylog.entry_date).select(:entry_date).last
     start_date = dutylog.entry_date

      if last_day_off.present?
        end_date = last_day_off.entry_date
      else
        end_date = dutylog.entry_date
      end

      a = user.dutylogs.where("entry_date >= ? AND entry_date <= ?", end_date, start_date).count
      a-1
   end
于 2013-07-17T15:44:09.047 回答