0

I want to construct a calendar full month view that looks like this (simplistically):

user        1   2   3   4   5   6   7   8   9   10   ...
user one            X           X       X
user two    X           X   X

Where 1..31 across the top is the calendar dates

This is sourced from the following models

user has_many leave_requests

leave_request belongs_to user
leave_request has_many leave_days

leave_day belongs_to leave_request

Currently, the only way I can get this to work is brute force it like this (this is pseudo-coded to save space - it's not the way I want to do it):

<%
@users = User.all
@users.each do |user|
%>
  <tr>
      <td><%= user.name %></td>
      <% (1..31).each do |day| %>
        <% user.leave_request.each do |lr| %>
          <% lr.leave_dates.each do |ld| %>
            <% if ld.date_requested == day %>
              <% text = day.date_requested %>
            <% else %>
              <% text = "" %>
            <% end %>
            <td><%= text %></td>
          <% end %>
        <% end %>
      <% end %>
  </tr>
<% end %>

I guess, what I'm really looking for is a way to map the dates from the leave_days table onto a calendar month. I don't mind a bit of complexity but this brute force approach just feels wrong

Craig McGuff
  • 3,968
  • 6
  • 30
  • 35

0 Answers0