Rails. How to store time of day (for schedule)?

前端 未结 8 769
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 02:25

I\'m writing an app that keeps track of school classes.

I need to store the schedule. For example: Monday-Friday from 8:am-11am.

I was think

8条回答
  •  孤街浪徒
    2020-12-13 02:43

    I made an app recently that had to tackle this problem. I decided to store open_at and closed_at in seconds from midnight in a simple business hour model. ActiveSupport includes this handy helper for finding out the time in seconds since midnight:

    Time.now.seconds_since_midnight
    

    This way I can do a simple query to find out if a venue is open:

    BusinessHour.where("open_at > ? and close_at < ?", Time.now.seconds_since_midnight, Time.now.seconds_since_midnight)
    

    Any tips for making this better would be appreciated =)

提交回复
热议问题