Skip to content

Instantly share code, notes, and snippets.

@nz
Last active December 22, 2015 05:59
Show Gist options
  • Save nz/6427811 to your computer and use it in GitHub Desktop.
Save nz/6427811 to your computer and use it in GitHub Desktop.
Multi model search with Sunspot

Create a search controller on the command line

rails generate controller search

Add a route for /search in config/routes.rb

get 'search' => 'search#search', :as => :search

Create the search method in app/controllers/search_controller.rb

class SearchController < ApplicationController

  def search
    @search = Sunspot.search(Article, Author, Comment) do
      keywords params[:q]
      # the rest of your search
    end
  end

end

Render the results in app/views/search/search.html.erb

<%= render @search.results %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment