我有一个Rails 5应用程序,列出了一系列事件。我已经很好地设置了SearchKick,我现在正在尝试添加仅显示今天事件的功能,但我正在努力实现。我目前有以下内容:
events_controller.rb
class EventsController < ApplicationController
def index
query = params[:j].presence || "*"
conditions = {}
conditions[:sector_name] = params[:sector_name] if params[:sector_name].present?
conditions[:service_name] = params[:service_name] if params[:service_name].present?
conditions[:start_date] = params[:start_date] if params[:start_date].present?
@events = Event.search query, where: conditions, aggs: [:sector_name, :service_name], range: {start_date: {"gte": "now/d"}}, order: {start_date: {order: "asc", unmapped_type: "long"}}, page: params[:page], per_page: 10
end
...
end
start_date
在我的postgres db中属于date
类型。
当我转到index.html.erb
时,我仍然在今天的日期之前看到start_date
的事件。
答案 0 :(得分:1)
完成了工作。
def index
query = params[:j].presence || "*"
conditions = {start_date: {"gte": "now/d"}}
conditions[:sector_name] = params[:sector_name] if params[:sector_name].present?
conditions[:service_name] = params[:service_name] if params[:service_name].present?
conditions[:start_date] = params[:start_date] if params[:start_date].present?
@events = Event.search query, where: conditions, aggs: [:sector_name, :service_name], order: {start_date: {order: "asc", unmapped_type: "long"}}, page: params[:page], per_page: 10
end
我只是将start_date: {"gte": "now/d"}
传递给条件哈希。这意味着它现在可以与我的aggs搜索结合使用。