Reputation: 315
I have a Rails 5 app that lists a collection of events. I have SearchKick set up nicely and I am now trying to add functionality that only displays the events from today onwards but I'm struggling with the implementation. I currently have the following:
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
is of date
type in my postgres db.
When I go to index.html.erb
I'm still seeing events with a start_date
before today's date.
Upvotes: 1
Views: 1143
Reputation: 315
Worked it out.
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
I simply passed start_date: {"gte": "now/d"}
into the conditions hash. This means it now works in conjunctions with my aggs search.
Upvotes: 1