I have an Organiser model, which has has_many :events
. The model looks like the following:
class Organiser < ApplicationRecord
has_many :events, dependent: :delete_all
scope :search_import, -> { includes(:events) }
searchkick searchable: [:name]
def search_data
{
name: name,
category: category
}
end
end
The Event model has a starts_on:datetime
column and I'd like to filter the organisers if it has an event within a date period. How would I achieve this?
EDIT
I've got this working by adding the following to the search_data
method:
events: events.map{|event| {starts_on: event.starts_on} }
and then in the search
method you can do the following:
where: {"events.starts_on" => {gt: Time.now}}
Seems to work well. If there is a better way of doing this then please mention it.
search_data
method:events: events.as_json(only: [:starts_on])
Pretty much the same, but makes it a little bit easier if you want to index other attributes from the association later. Note, if you want to filter by a date range, you can use:where: {"events.starts_on" => (date_range_start..date_range_end)}
- Mike