mysql - Rails and database structuring -
i have database of places (labs). each of them have opening , closing hour. new please comment on you, more experience mate, differently.
i decided create model called openday day:string, from_time: time, to_time: time.
my main question related displaying days. form insert database record looks this:
if left in picture creates 2 records in db, displayed this:
if from_time = 00:00 , to_time = 00:01 displayed "closed".
now want implement feature show every lab opened , the question is:
what more efficient - reimplementing form 7 records saved , mysql query display opened labs, or leave (in case) 2 records , implement function posted below (that checks every lab if opened)? in advance!
def opened_today?(lab) weekdays = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"] if !lab.open_days.empty? days_hash = {} current_day = nil weekdays.each |week_day| lab.open_days.each |open_day| if open_day.day == week_day days_hash[week_day] = {from_time: open_day.from_time, to_time: open_day.to_time} current_day = open_day else days_hash[week_day] = {from_time: current_day.from_time, to_time: current_day.to_time} end end end # dates variables: today = date.parse(time.zone.now.to_s).strftime("%a").downcase from_time = days_hash[today][:from_time].strftime("%h%m") to_time = days_hash[today][:to_time].strftime("%h%m") time_now = time.now.strftime("%h%m") if from_time < time_now && to_time > time_now return true else return false end end end


Comments
Post a Comment