我有兩個型號employee
和in_outs
。這些之間的關聯是employee
has many
in_outs
和in_out
belongs to
employee
。我想要顯示所有employees
的attendance
。爲此我的current logic
是這樣的。什麼是查詢的最佳方式?
邏輯控制器行爲:
def view_all_employee_attendance
employees = Employee.all
@all_employess_punch_in_outs = []
employees.each do |employee|
@all_employess_punch_in_outs << employee.in_outs.where('date >= ? and date <= ?', Date.today.at_beginning_of_month, Date.today)
end
end
並鑑於:
<tbody>
<% @all_employess_punch_in_outs.each do |punch_record| %>
<tr>
<td><%= punch_record.employee.name %></td>
<td><%= punch_record.check_in %></td>
<td><%= punch_record.check_out %></td>
</tr>
<% end %>
</tbody>
在這種情況下
在我view
再次queries
正在執行。如何使用eagerloading
在view
和action
中查詢optimise
?
你的問題有一些代碼格式問題。 – meshpi