Search code examples
ruby-on-railsscopesearchkick

Searchkick filter with scope


Has anybody used Searchkick with a scoped model?

I have an Article model with scope :published, ->{ where(status: "Published") }. So In my ArticleController I did the following: query = params[:q].presence || "*" @search = Article.published.search(query, operator: "or", suggest: true)

but it didn't work https://github.com/ankane/searchkick/issues/140 and kept including all the records

So I did: query = params[:q].presence || "*" @search = Article.search(query, where:{status: "Published"}, operator: "or", suggest: true)

Didn't work either... and returned nothing. Not sure what I am doing wrong.


Solution

  • I just needed to add status to the search_data hash

    class Article < ActiveRecord::Base
    
      searchkick highlight: [:title], text_start: [:title], language: "spanish"
    
      scope :draft,     ->{ where(status: "Draft") }
      scope :published, ->{ where(status: "Published") }
      scope :scheduled, ->{ where(status: "Scheduled") }
    
      def search_data
        {
          title: title,
          content: content,
          status: status     <--------
        }
      end