我在Rails应用程序上使用ElasticSearch和chewy GEM。我想帮你翻译一下:
all.select { |task|
(task.start_at.to_date..task.end_at.to_date).cover?(Date.today)
}
要使用带有嚼的滤镜。我试着用很长的时间来完成这项工作,但没有成功。
任何形式的帮助都是很好的。
谢谢
发布于 2015-09-17 14:17:56
我知道已经很长时间了,但我现在正在使用Chewy gem,所以它是这样的:
假设您有一个TasksIndex
,其中的start_at
和end_at
字段定义良好,属于date
类型,并且具有正确的
field :start_at, type: 'date', format: 'basic_date_time_no_millis' # example, you can use other formats
field :end_at, type: 'date', format: 'basic_date_time_no_millis'
那么你的过滤器可以是这样的:
def tasks_including_date(target_date)
formatted_target_date = target_date.strftime('%Y-%m-%d')
TasksIndex.filter{start_at <= formatted_target_date}.filter{end_at >= formatted_target_date}
end
然后你就可以:
tasks_including_date(Date.today)
如果您想要结果本身,而不仅仅是过滤范围,只需在结果上使用to_ary
即可。它会对你的结果进行处理。
发布于 2014-12-13 12:43:08
这段代码遍历集合"all“。对于每一项,如果今天在任务的执行时间范围内(它今天的日期在begin_at和end_at范围之间),即是否应该执行任务,它将检查并返回true。
https://stackoverflow.com/questions/27454207
复制相似问题