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:
In this case, I would probably do something relational, perhaps with STI if you wanted to have certain days where the business is closed (e.g. non-recurring closings). Here's a basic STI example:
class Business < ActiveRecord::Base
has_many :open_time_blocks
has_many :closed_time_blocks
def open?(time)
# false if time is "inside" any of the closed_time_blocks
# else is true if inside any of the open_time_blocks
# else is false
end
def closed?(time)
!open?
end
end
# == Schema Information
#
# Table name: time_blocks
#
# id :integer not null, primary key
# business_id :integer
# type :string(255)
# start_at :datetime
# end_at :datetime
# created_at :datetime
# updated_at :datetime
class TimeBlock < ActiveRecord::Base
belongs_to :business
end
class OpenTimeBlock < TimeBlock; end
class ClosedTimeBlock < TimeBlock; end