I would like to store an display OpeningHours for stores like this slots example:
mon = 9h00 => 12h00, 14h30 => 20H00
tue = 9H00 => 20H00
we = 14h15 => 20h00
thu = closed
I need to use it for OpeningOursSpecifications from Schema.org
I would like to store an display OpeningHours for stores like this slots example:
mon = 9h00 => 12h00, 14h30 => 20H00
tue = 9H00 => 20H00
we = 14h15 => 20h00
thu = closed
I need to use it for OpeningOursSpecifications from Schema.org
3
Answers
Interesting question…
I would create models as follows:
I definitely would not use jsonb for the hours, why not use columns? In my experience there are always downsides to jsonb that come up later (searching records, changing values, sorting… etc)
The enum column is optional, it gives you some nice methods to use.
Rails does not support JSONB well, and this is a perfect fit for a conventional relation.
However, do not create a table with 28 columns. If you find yourself making lists as columns, you probably have a design problem. Instead, make a table with one row for each day-of-week/open/close time.
This lets a store have as complicated an open/close time as they want.
See Building Complex Forms for how to write the controller and view to add, edit, and remove StoreHours.
Some comments:
"monday"
but it is stored as a number to save space and time and to prevent typos. I’ve made the mapping between strings and numbers explicit in case more days need to be added (for example: weekends, holidays).open_at: 12pm, open_for: 13 hours
means from noon to 1am the next day. This is more complicated, but also more flexible.has_many :store_hours, dependent: :delete
to ensure when a store is destroyed its StoreHours are also deleted.For relational database use 5 columns table:
Treat the store as closed by default and store only records with other types (like opened).
If stores and users are in different time zones you may store period_begin/end in UTC and calculate and show local time and day of a week accordingly user’s timezone.