简体   繁体   English

Rails Active Record查询工作日总计离开

[英]Rails Active Record Query Total leaves on a weekday

The scenario is that there is hotel and it has so many employees. 场景是有一家旅馆,它有那么多员工。 now employees can apply for the leaves. 现在员工可以申请休假了。

Leaves table have the start and end date column suppose a employee takes leave from 5, December to 15 December than start date will be 5 December and end date will be 15 December. 休假表中的开始日期和结束日期列假设员工在12月5日至12月15日休假,而开始日期为12月5日,结束日期为12月15日。

How can I get the total leaves in 2017 on particular weekday like on Monday? 如何获得特定工作日(如星期一)的2017年总叶子?

Found the solution: 找到了解决方案:

def calculate_weekdays
count_weekday = {}
(0..6).to_a.select{|t| count_weekday[t]=0 }
Event.all.each do |event|
  start_date, end_date = event.start_time, event.end_time
  result = ( start_date.to_date..end_date.to_date ).to_a.select { |k| count_weekday[k.wday]=count_weekday[k.wday]+1}
end
return count_weekday
end

I think you can use gem groupdate 我认为您可以使用gem groupdate

With a query like: 带有如下查询:

Leave.where( ... ).group_by_day_of_week(:start_date).count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM