manpower
与attendance
有一个_to_many关联
invoice
表具有属性from_date
、to_date
attendance_table
具有属性status
、manpower_id
和attance_date
我能够提供发票#索引上的所有人力。但我不知道如何计算每个人力的出勤率,并将其显示在列中,同时考虑到发票表中的持续时间
在考勤表1中。并且0不存在。
<table>
<thead>
<tr>
<th>Name</th>
<th>total present</th>
<th>Total Abesent</th>
</tr>
</thead>
<tbody>
<% @manpowers.each do |manpower| %>
<tr>
<td><%= manpower.name %></td>
<td><%= "" %></td>
<td><%= "" %></td>
</tr>
<% end %>
</tbody>
</table>
目前-manpower.attendance.where(attendance_date: from_date..to_date, status: 1).count
对于缺席者-manpower.attendance.where(attendance_date: from_date..to_date, status: 0).count
您可以在考勤模型中创建范围,并在视图中使用这些范围。
class Attendance < ActiveRecord::Base
scope :absent, where(status: 0)
scope :present, where(status: 1)
scope :date_between, -> (from_date, to_date) { where(attendance_date: from_date..to_date) }
end
并像一样使用它
目前-manpower.attendance.date_between(from_date, to_date).present.count
对于缺席者-manpower.attendance.date_between(from_date, to_date).absent.count
<table>
<thead>
<tr>
<th>Name</th>
<th>Total Present</th>
<th>Total Absent</th>
</tr>
</thead>
<tbody>
<% @manpowers.each do |manpower| %>
<tr>
<td><%= manpower.name %></td>
<td><%= manpower.attendance.date_between(from_date, to_date).present.count %></td>
<td><%= manpower.attendance.date_between(from_date, to_date).absent.count %></td>
</tr>
<% end %>
</tbody>
</table>