Alex Rivera | Logout

How would you store a business's hours in the db/model of a Rails app?

Asked 2010-07-28T00:22:03.593
18

I'm creating a Rails app that will store the opening and closing hours for a business. Originally, I thought of simply using a text data type and letting it be free-form:

"Monday to Friday 9am to 5pm
Saturday 11am to 4pm
Closed Sundays"

But, requirements have changed and I need to check the hours against the current date & time and display an "Open" or "Closed" in the view. Something like:

class Business < ActiveRecord::Base

  def open?
    # Something like ... 
    Time.now > open_time && Time.now < close_time
  end

end

So what would be the best way to tackle this in terms of storing the hours for each day of the week? Should the Business simply has_many :open_blocks (or whatever) that have open and close times? Should I just store the day as a string?

Edit
Report

1 Answer

0

Checkout these libraries for handling recurring dates.

Once the recurrence is setup in an object say office_hours, in ice_cube you can query it as:

office_hours.occurring_at?(Time.now)
answered 2010-07-28T00:38:10.033

Your Answer