在我的activeadmin上,我必须给每个商店打开和关闭几个小时。 例如,当商店从晚上9点到凌晨04点营业时,它被记录为同一日期。 但我的算法有故障
我的算法是:
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
我尝试为&#39; 1.day&#39;添加gem activesupport。但是我有一个超出范围的错误&#39; 我找不到解决方案,你能帮我快点吗?
答案 0 :(得分:0)
yerderday_day必须是一个月中的某一天(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