HABTM Loops

So, I have a “schedules” table and a “hours” table, joined by a
“hours_schedules” table. Think of the hours table as a category for
when people will schedule themselves to work, which happens in weekly
increments. At any given hour, there will be X number of people
working. I need to represent each worker with an asterisk symbol in
the weekly view. For example,

PEOPLE WORKING

Sunday

12:00 am: **
1:00 am: *****
2:00 am: ******
3:00 am: ******
etc…

How can I create a loop that can replace my extremely un-DRY queries
in my controller?

Controller

@contract1_schedules = Hour.find(1).schedules.find(:all, :conditions
=> “week = ‘#{@woy}’”)
@contract2_schedules = Hour.find(2).schedules.find(:all, :conditions
=> “week = ‘#{@woy}’”)
@contract3_schedules = Hour.find(3).schedules.find(:all, :conditions
=> “week = ‘#{@woy}’”)
@contract4_schedules = Hour.find(4).schedules.find(:all, :conditions
=> “week = ‘#{@woy}’”)
@contract5_schedules = Hour.find(5).schedules.find(:all, :conditions
=> “week = ‘#{@woy}’”)
etc…
@contract168_schedules =
Hour.find(168).schedules.find(:all, :conditions => “week = ‘#{@woy}’”)

@woy is a calculated value for next week and is compared to the week
number stored with the parent schedule so that I can display only a
certain week at a time. Each of these queries represent a collection
of schedules for that hour.

#################################

View

People Working

Sunday

<%= link_to “12:00am”, :controller => “hours”, :action => ‘list’, :id
=> ‘1’ %>:
<% for contract12_schedule in @contract12_schedules %>
*
<% end %>

<%= link_to “1:00am”, :controller => “hours”, :action => ‘list’, :id
=> ‘2’ %>:
<% for contract1_schedule in @contract1_schedules %>
*
<% end %>

etc…
#################################

Schema

mysql> show tables;
±------------------------------+
| Tables_in_sandbox_development |
±------------------------------+
| contracts |
| hours |
| hours_schedules |
| schedules |
| statuses |
±------------------------------+

mysql> describe schedules;
±------------±-------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±------------±-------------±-----±----±--------±---------------+
| id | int(11) | NO | PRI | NULL | auto_increment
|
| contract_id | int(11) | YES | | NULL |
|
| week | int(11) | YES | | NULL |
|
| year | int(11) | YES | | NULL |
|
| name | varchar(255) | YES | | NULL |
|
±------------±-------------±-----±----±--------±---------------+
5 rows in set (0.01 sec)

mysql> describe hours;
±------±-------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------±-------------±-----±----±--------±------+
| id | int(11) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
±------±-------------±-----±----±--------±------+
2 rows in set (0.00 sec)

mysql> describe hours_schedules;
±------------±--------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------------±--------±-----±----±--------±------+
| schedule_id | int(11) | YES | | NULL | |
| hour_id | int(11) | YES | | NULL | |
±------------±--------±-----±----±--------±------+
2 rows in set (0.00 sec)

It finally clicked for me…

View

Sunday

<% for hour in @sunday_hours %>
<% @schedule_hours =
Hour.find(hour.id).schedules.find(:all, :conditions => “week =
‘#{@woy}’”) %>
<%= link_to “#{hour.name}”, :controller => “hours”, :action =>
‘list’, :id => hour.id %>:
<% for schedule in @schedule_hours %>
*
<% end %>


<% end %>

Controller

def list
# Finds next weeks beginning on Sunday
@now = Time.now
@next_week = @now.next_week - 1.day
@woy = @next_week_date.cweek

# Creates an array of hours for each day of the week
@sunday_hours = Hour.find(:all, :conditions => "id > '0' AND id <

‘25’")
@monday_hours = Hour.find(:all, :conditions => “id > ‘24’ AND id <
‘49’”)
etc…
end