Search code examples
ruby-on-railsrubysearchruby-on-rails-4sunspot

How to search only approved articles using sunspot


I have a article model that has a default status "Awaiting" and in my article model I have:

  scope :awaiting, -> {where(status: "Awaiting") }
  scope :approved, -> {where(status: "Approved") }
  scope :rejected, -> {where(status: "Rejected")}

and to allow searching only approved articles I have this in my article model:

searchable do
  text :title
  string :status
end

and this is my search controller

  def search

    @search = Sunspot.search(Article) do
        fulltext params[:query]
        with(:status, "Approved")
        order_by :score, :desc
        paginate(page: params[:page], per_page: 10)

    end

    @articles = @search.results

    respond_to do |format|
      format.html { render :action => "search" }
    end
  end

But when I'm trying to search anything this will turn no result, why this?


Solution

  • If your models already existed before adding Sunspot you will need to create the index with bundle exec rake sunspot:solr:reindex.

    Future updates should automatically be added to the index.