ruby on rails - Checking a hash for the existence of a matching value -
i'm building school calendar lists classes ("sections") , displays semester , exam dates classes. calendar fed hash defined in section.rb
model:
def get_calendar calendar = {} calendar[:semesters] = semester.where(section_id: self.id) calendar[:exams] = exam.where(section_id: self.id) { :calendar => calendar } end
the semester
, exam
objects have name
, start_date
, end_date
.
when looping through each day of calendar, how can check if there's semester or exam a start_date
or end_date
for day of loop?
if there semester or exam either start_date
or end_date
matches calendar date, display name.
the calendar dates , start_date
, end_date
fields use date
class (link).
i sincerely appreciate help. can clarify question if needed :)
thank you!
maybe should re-think hash, , use way:
def get_calendar semesters = self.semesters exams = self.exams events = { } (semesters + exams).each |event| start_date = event.start_date.to_s events[start_date] ||= [] events[start_date] << event end_date = event.end_date.to_s events[end_date] ||= [] events[end_date] << event end events end
and test presence of event in loop constructs calendar:
dates_of_calendar.each |date| if @events[date.to_s].present? # well, there event happening date! treat wishes! end # etc... end
Comments
Post a Comment