# Listing employees, categorized by license expiration date

I am working on a personal project and I have hit a wall. I know I am writing bad code and I really want to refactor the code below. The application has three tables on the same page. Each table contains data from a has-many-though relationship. In essence I have an employees page which contains three tables of employee licenses that all expire in grouped intervals:

ALL EMPLOYEE PAGE

• Employees with licenses Expiring in 30 days
• Employees with licenses Expiring in 30-90 days
• Employees with licenses Expiring in 90 days

All three of these tables are independently paginated and I am allowing the user to enter a search term and search across all three tables.

However I have over 1200 licenses so the page is taking forever to load. How can I optimize this functionality? Any help would be greatly appreciated.

## Model

  def self.emp_lic_small
end

def self.emp_lic_medium
end

def self.emp_lic_large
end


## Controller

    @small_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_small.paginate(:page => params[:small_lic], :per_page => 20)
@medium_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_medium.paginate(:page => params[:med_lic], :per_page => 20)
@large_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_large.paginate(:page => params[:large_lic], :per_page => 20)


## View

<div class="panel panel-danger">
<table class="table">
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
<tbody>
<tr>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>

<div class="panel panel-warning">
<table class="table">
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
<tbody>
<tr>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<div class="panel panel-success">
<table class="table">
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
<tbody>
<tr>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>

• I'd wager the bottleneck is in the model methods. You're mapping all records. I can't tell from the code exactly what you're scoping that to – what model is this from? And what's se? Are the lists scoped to something before searching? – but you're looking for licenses, but seem to be going the long way of finding licenses through something. It also implies that the pagination happens late - after a lot of stuff has been loaded. So the pagination is the act of throwing a lot of it away, rather than loading what you need. But your question doesn't have enough detail, so I'm not sure. – Flambino Jan 8 '17 at 19:41
• Could you post how these tables are related? (the has_many, etc, for the tables in the example?) – lcguida Jan 26 '17 at 10:48
You should probably reduce the number of database queries, first you repeat the SiteEmployee search 3 times. Do it once and store it in a a variable. I would also query the database once with an order clause together with the Enumerable group_by method to get all of the results in one go.