在我的activeadmin上,我必须给每个商店几个小时的营业时间。
例如,当一家商店从晚上9点到凌晨4点营业时,它被记录为同一日期。
但我的算法有问题
我的算法是:

  def opened?
    today_day = Date.today.wday
    yesterday_day = today_day == 0 ? 6 : (today_day - 1)
    opening = self.openings.where(day: [today_day, yesterday_day]).first
    if opening
      opening_day = (Time.now).day
      if opening.closes_at < opening.opens_at
        opening_day = yesterday_day
      end
      # binding.pry
      today_opens_at = Time.new((Time.now).year, (Time.now).month, opening_day, opening.opens_at.hour, opening.opens_at.min)
      today_closes_at = Time.new((Time.now).year, (Time.now).month, (Time.now).day, opening.closes_at.hour, opening.closes_at.min)
      if today_opens_at < (Time.now) && (Time.now) < today_closes_at
        true
      else
        false
      end
    else
      false
    end
  end

我试图为“1.day”添加gem activesupport,但有一个错误“参数超出范围”
我找不到解决办法,你能帮我快点吗?

最佳答案

日必须是一个月中的一天(0..30)

     today_day_of_week = Date.today.wday
     yesterday_day_of_week = today_day_of_week == 0 ? 6 : (today_day_of_week - 1)
     opening = self.openings.where(day: [today_day_of_week, yesterday_day_of_week]).first
     yesterday_day = Date.today.prev_day.day

或者换掉这个方块:
  if opening.closes_at < opening.opens_at
    opening_day = Date.today.prev_day.day
  end

10-04 20:49