I have a loop through a hash of times with a nested Booking class. I want it to make a table of times from the hash and say "Available" or "Booked" if there is a booking object with the same hour Time as the value of the hash.
index.html.erb
table
<% @times.each_with_index do |(key, value), index| %>
tr
<td><%= key %></td>
<% for booking in @bookings %>
<% if booking.time.strftime("%H").to_s == value.to_s then %>
<td> <em> Booked </em> </td>
<% end %>
<% end %>
<% if booking.time.strftime("%H").to_s != value.to_s then %>
<td> <em> Available </em> </td>
<% end %>
/tr
<% end %>
/table
This produces this:
9am Available
10am Available
11am Available
12am Available
1pm Booked Available
2pm Available
3pm Available
4pm Booked
5pm Available
6pm Available
7pm Available
8pm Available
9pm Available
So it is repeating the loop on 1pm because that value has change the second time round. How can I avoid this happening?