【发布时间】:2014-08-13 05:37:40
【问题描述】:
现在我正在尝试查看某个节目的开始和结束时间是否与另一个正在录制的节目重叠 => true 其中“节目”是用户想要录制的电视节目。
def self.record_show
shows = Box.first.shows.where(:recording => true).flatten
show_start_and_end_times = shows.collect {|x| x.start_time..x.end_time}
current_show_time = show.start_time..show.end_time
overlap = show_start_and_end_times.select {|c| current_show_time.overlaps?(c)}
if overlap.present?
nil
else
show.update_attributes(:recording => true)
show.save
end
end
它运行该方法,但我很难弄清楚如何获取它,以便它找到导致重叠的实际当前录制节目。例如,假设在“节目”中我目前有两个节目:
[#<Show id: 181, box_id: 78, title: "The Fox", channel: 22, single_recording: true, created_at: "2014-08-12 19:55:49", updated_at: "2014-08-12 20:09:24", start_time: "2014-08-12 19:55:49", end_time: "2014-08-12 20:25:49", recording: true>, #<Show id: 186, box_id: 78, title: "Funniest Home Videos", channel: 45, single_recording: true, created_at: "2014-08-12 19:55:49", updated_at: "2014-08-12 20:09:27", start_time: "2014-08-12 23:20:49", end_time: "2014-08-13 00:20:49", recording: true>]
在 show_start_and_end_times 我有:
[Tue, 12 Aug 2014 19:55:49 UTC +00:00..Tue, 12 Aug 2014 20:25:49 UTC +00:00, Tue, 12 Aug 2014 23:20:49 UTC +00:00..Wed, 13 Aug 2014 00:20:49 UTC +00:00]
在 current_show_time 我有:
Tue, 12 Aug 2014 19:55:49 UTC +00:00..Tue, 12 Aug 2014 20:55:49 UTC +00:00
这意味着在重叠中我有第一个 show_start_and_end_times 节目的 start_time..end_time,这是导致重叠的原因:
[Tue, 12 Aug 2014 19:55:49 UTC +00:00..Tue, 12 Aug 2014 20:25:49 UTC +00:00]
我尝试将这两次与另一个进行比较:
(shows.first.start_time..shows.first.end_time) == (overlap.first)
这给了我错误,即使时间完全相同。如何将重叠时间与节目列表进行比较,以找出导致重叠的节目?
【问题讨论】:
标签: ruby-on-rails ruby