Search code examples
ruby-on-railssearchkick

undefined method `where` for searchkick


I'm trying to add a date range filter to my searchkick

This is what i have

    @events = Event.page(params[:page]).per(10).search(params[:search], misspellings: { distance: 1 }, order: { date: :asc, eventname: :asc }, match: :word_start, page: params[:page], per_page: 20)
    if params[:date_from]
      byebug
@events = @events.where('date BETWEEN ? AND ?', params[:date_from], params[:date_to])
    end

However the issue i'm getting is this:

*** NoMethodError Exception: undefined method `where' for #<Searchkick::Results:0x007f95eaf97f90>

Any help would be greatly appreciated

Edit

I know this is potentially better in another question but its kinda the same question

 def search
    @events = Event.page(params[:page]).per(10)
    if params[:date_from]
        @events = @events.where('date between ? AND ?', params[:date_from], params[:date_to])
    byebug
    end

    @events = @events.search(params[:search], misspellings: { distance: 1 }, order: { date: :asc, eventname: :asc }, match: :word_start, page: params[:page], per_page: 20)
    if @events.results.any?
      render 'events/results'
    end
end

Now this isn't working how i want it, I've got the event name, the datefrom and to being passed through on the params. If i type @events on the byebug it gets events in the range, but doesnt get the event i need


Solution

  • it looks like searchkick uses it's own query syntax. so something like this might help

    search_opts = {
      misspellings: { distance: 1 }, 
      order: { date: :asc, eventname: :asc },
      match: :word_start, 
      page: params[:page],
      per_page: 20
    }
    if params[:date_from]
       search_opts[:where] =  { date: {gte: params[:date_from], lte: params[:date_to]} }
    end
    
    @events = Event.search params[:search], search_opts