Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method "results" with Sunspot Solr Search

I am using Rails 3.1 and have been using this railscast tutorial to implement sunspot. I am following everything right (i think) however when I run the search like this:

class ProductsController < ApplicationController
  # GET /products
  # GET /products.xml

  def index
    @search = Product.search do
      fulltext params[:search]
    end
    @products = @search.results
    respond_to do |format|
      format.html
      format.xml  { render :xml => @products }
    end
  end...

Here's how I have declared the searchable in my product.rb file

searchable do
    text :title
end

However I keep running in to the following error

undefined method `results' for #<MetaSearch::Searches::Product:0x12a089f50>

But when I do just a @products = @search, i get a full list of all the products, no matter what i send in the search query

Anyone have any idea what I am doing wrong?

like image 571
alik Avatar asked Oct 28 '11 06:10

alik


2 Answers

Are you sure there are no conflicts with other search gems? I can't test it at the moment, but I'm fairly sure Sunspot doesn't use MetaSearch::Searches. However, this gem does: https://github.com/ernie/meta_search/.

Have you tried doing this instead?

@search = Sunspot.search(Product) do
  fulltext params[:search]
end

That way you can be sure that it uses Sunspot to search and not some other gem. Also if you need more searching gems then put Sunspot above them in the gemfile.

like image 188
amunds Avatar answered Sep 20 '22 06:09

amunds


Sunspot will refuse to define the class search method if the class already has one defined. You can instead use the solr_search method to the same effect.

like image 24
Nick Zadrozny Avatar answered Sep 18 '22 06:09

Nick Zadrozny