简体   繁体   中英

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.

How can I get the total leaves in 2017 on particular weekday like on Monday?

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

With a query like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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