没有预约的群
Group where no appointment
下面的代码显示每小时的预约次数,如果没有预约,则 'No appointment'。
我想根据时间
对连续 'No appointment' 个槽进行分组
例如,当前代码的输出是-
10:00 AM to 11:00 AM -> No Appointment
11:00 AM to 12:00 PM -> No Appointment
12:00 PM to 01:00 PM -> No Appointment
我希望输出为
10:00 AM to 01:00 PM -> No Appointment
代码如下:-
<% @time_group = ['6:00:00','7:00:00','8:00:00','9:00:00','10:00:00','11:00:00','12:00:00','13:00:00','14:00:00','15:00:00','16:00:00','17:00:00','18:00:00','19:00:00','20:00:00','21:00:00','22:00:00','23:00:00'] %>
#Loop through each hour in @time_group
<% @time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% @booked = Appointment.where(start_time: t1..t2 %>
#Show time interval e.g 1:00 PM to 2:00 PM
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
#if appointment is available show appointment else show no appointment
<% if @booked.size > 0 %>
<% @booked.each do |app| %>
<%= app.start_time.strftime('%I:%M') %>
<% end %>
<% else %>
No Appointment
<% end %>
<% end %>
这样试试
<% @time_group = ['6:00:00','7:00:00','8:00:00','9:00:00','10:00:00','11:00:00','12:00:00','13:00:00','14:00:00','15:00:00','16:00:00','17:00:00','18:00:00','19:00:00','20:00:00','21:00:00','22:00:00','23:00:00'] %>
#Loop through each hour in @time_group
<% @time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% @booked = Appointment.where(start_time: t1..t2) %>
#It will holds the No Appointment Timings into it
no_appointment_array = []
#if appointment is available show appointment else show no appointment
<% if @booked.size > 0 %>
#Calling the Function
display_no_appointment(no_appointment_array) if not no_appointment_array.empty?
#Show time interval e.g 1:00 PM to 2:00 PM
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
<% @booked.each do |app| %>
<%= app.start_time.strftime('%I:%M') %>
<% end %>
<% else %>
no_appointment_array << [Time.parse(t).strftime('%I:%M %p'), (Time.parse(t) + 3600).strftime('%I:%M %p')]
<% end %>
<% end %>
def display_no_appointment(no_appointment_array)
puts "#{no_appointment_array.first[0]} to #{no_appointment_array.last[1]} -> No Appointment"
#It Clears all Previous No Appointment
no_appointment_array.clear
end
首先我建议你使用 if '@booked.present?'而不是 'if @booked.size > 0'
你试过这样的事情吗?
我没有测试这段代码,它几乎是一个可能的解决方案的想法。
希望对您有所帮助。
<% no_appointment_starts_at = nil %>
<% no_appointment_ends_at = nil %>
<% @time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% @booked = Appointment.where(start_time: t1..t2 %>
#Show time interval e.g 1:00 PM to 2:00 PM
#if appointment is available show appointment else show no appointment
<% if @booked.present? %>
<% @booked.each do |app| %>
<% unless no_appointment_ends_at.blank? %>
<%= Time.parse(no_appointment_starts_at).strftime('%I:%M %p') %> to <%= (Time.parse(no_appointment_ends_at) + 3600).strftime('%I:%M %p') %>
<% no_appointment_ends_at = nil%>
<% end %>
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
<%#= app.start_time.strftime('%I:%M') %>
<% no_appointment_starts_at = t2 %>
<% end %>
<% else %>
<% last_appointment_ends_at = t2 %>
<% end %>
<% end %>
首先,可以准备数组:
@time_group = ['6:00:00','7:00:00', ...]
@stacked = @time_group.each_with_object([]) do |start, memo|
range = Time.parse(start)..Time.parse(start)+3600
booked = Appointment.where(start_time: range)
if memo.empty? || !booked.empty? || !memo.last.last.empty?
memo << [range, booked] # no sequenced empty slots
else
memo << [memo.pop.begin..range.end, booked] # merge ranges
end
end.to_h # { ranges => booked }
现在——让我们输出它们:
<% @stacked.each do |range, booked| %>
.....
<% end %>
下面的代码显示每小时的预约次数,如果没有预约,则 'No appointment'。
我想根据时间
对连续 'No appointment' 个槽进行分组例如,当前代码的输出是-
10:00 AM to 11:00 AM -> No Appointment
11:00 AM to 12:00 PM -> No Appointment
12:00 PM to 01:00 PM -> No Appointment
我希望输出为
10:00 AM to 01:00 PM -> No Appointment
代码如下:-
<% @time_group = ['6:00:00','7:00:00','8:00:00','9:00:00','10:00:00','11:00:00','12:00:00','13:00:00','14:00:00','15:00:00','16:00:00','17:00:00','18:00:00','19:00:00','20:00:00','21:00:00','22:00:00','23:00:00'] %>
#Loop through each hour in @time_group
<% @time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% @booked = Appointment.where(start_time: t1..t2 %>
#Show time interval e.g 1:00 PM to 2:00 PM
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
#if appointment is available show appointment else show no appointment
<% if @booked.size > 0 %>
<% @booked.each do |app| %>
<%= app.start_time.strftime('%I:%M') %>
<% end %>
<% else %>
No Appointment
<% end %>
<% end %>
这样试试
<% @time_group = ['6:00:00','7:00:00','8:00:00','9:00:00','10:00:00','11:00:00','12:00:00','13:00:00','14:00:00','15:00:00','16:00:00','17:00:00','18:00:00','19:00:00','20:00:00','21:00:00','22:00:00','23:00:00'] %>
#Loop through each hour in @time_group
<% @time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% @booked = Appointment.where(start_time: t1..t2) %>
#It will holds the No Appointment Timings into it
no_appointment_array = []
#if appointment is available show appointment else show no appointment
<% if @booked.size > 0 %>
#Calling the Function
display_no_appointment(no_appointment_array) if not no_appointment_array.empty?
#Show time interval e.g 1:00 PM to 2:00 PM
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
<% @booked.each do |app| %>
<%= app.start_time.strftime('%I:%M') %>
<% end %>
<% else %>
no_appointment_array << [Time.parse(t).strftime('%I:%M %p'), (Time.parse(t) + 3600).strftime('%I:%M %p')]
<% end %>
<% end %>
def display_no_appointment(no_appointment_array)
puts "#{no_appointment_array.first[0]} to #{no_appointment_array.last[1]} -> No Appointment"
#It Clears all Previous No Appointment
no_appointment_array.clear
end
首先我建议你使用 if '@booked.present?'而不是 'if @booked.size > 0'
你试过这样的事情吗? 我没有测试这段代码,它几乎是一个可能的解决方案的想法。 希望对您有所帮助。
<% no_appointment_starts_at = nil %>
<% no_appointment_ends_at = nil %>
<% @time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% @booked = Appointment.where(start_time: t1..t2 %>
#Show time interval e.g 1:00 PM to 2:00 PM
#if appointment is available show appointment else show no appointment
<% if @booked.present? %>
<% @booked.each do |app| %>
<% unless no_appointment_ends_at.blank? %>
<%= Time.parse(no_appointment_starts_at).strftime('%I:%M %p') %> to <%= (Time.parse(no_appointment_ends_at) + 3600).strftime('%I:%M %p') %>
<% no_appointment_ends_at = nil%>
<% end %>
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
<%#= app.start_time.strftime('%I:%M') %>
<% no_appointment_starts_at = t2 %>
<% end %>
<% else %>
<% last_appointment_ends_at = t2 %>
<% end %>
<% end %>
首先,可以准备数组:
@time_group = ['6:00:00','7:00:00', ...]
@stacked = @time_group.each_with_object([]) do |start, memo|
range = Time.parse(start)..Time.parse(start)+3600
booked = Appointment.where(start_time: range)
if memo.empty? || !booked.empty? || !memo.last.last.empty?
memo << [range, booked] # no sequenced empty slots
else
memo << [memo.pop.begin..range.end, booked] # merge ranges
end
end.to_h # { ranges => booked }
现在——让我们输出它们:
<% @stacked.each do |range, booked| %>
.....
<% end %>