Ask A Question

Notifications

You’re not receiving notifications from this thread.

How should I implement a week model with days and opening times?

Drilon Buzuku asked in Rails

Hello,

I need a week model which contains all weekdays and every weekday has multiple opening and closing times.
For example, a barber shop is open on every monday from 9 a.m. to 11 a.m. and 1 p.m. to 7 p.m.. Should i create a week model which has weekday fields, like this [1] and then serialize them as a hash?
[1]
def change
create_table :week do |t|
t.text:monday
t.text :tuesday
t.text :wednesday
.....
t.text:sunday
end
Pseudocode:
last_week = Week.last
last_week.monday = {:start_time => "9 a.m.", :end_time => "11 a.m."}
last_week.friday = {:start_time => "6 a.m.", :end_time => "11 p.m."}

How would you approach this?

Cheers,

Drilon

Reply

Hey Drilon,

It kind of depends on how flexible you need it to be. Will your days only ever need a maximum of 2 operating groups, ie for Monday, group 1 = 9am-11am and group 2 = 1pm-7pm? Or could some days need only one group (Saturday group 1 = 11am-3pm), and/or others need 3+ groups? I don't think your method would be too friendly with varying work groups, but I might be wrong.

Also, how often might the daily work hours need to be updated? Is that something the client will need access to, or would it be something they'd just refer back to you to update if their hours ever change? Will hours vary based on what week it is, or will they be the same regardless of the week #? By week #, I mean there are 52 weeks in a year, the first week in January is week 1, and the last week in December is week 52.

Consider how you're going to be querying for these hours and how you're going to be using those results. This can help figure out the best way to structure your model. Good thing with a setup like this, you can create a constant to test it before actually making the migration... so in your model, just do something like below, then you can start tinkering with the structure to see what works best for your needs.

class ShopOption < ActiveRecord::Base

    # query with: ShopOption::SHOPHOURS[:monday][:open] would return "11:30:00"

    SHOPHOURS = {
            sunday: {
                open: '08:30:00',
                close: '14:30:00',
                status: 'open'
            },
            monday: {
                open: '11:30:00',
                close: '19:30:00',
                status: 'open'
            },
            tuesday: {
                open: '11:30:00',
                close: '19:30:00',
                status: 'open'
            },
            wednesday: {
                open: '11:30:00',
                close: '19:30:00',
                status: 'open'
            },
            thursday: {
                open: '11:30:00',
                close: '19:30:00',
                status: 'open'
            },
            friday: {
                open: '11:30:00',
                close: '19:30:00',
                status: 'open'
            },
            saturday: {
                open: '08:30:00',
                close: '18:30:00',
                status: 'open'
            }
        }

    end
    ```
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,140+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.