alik
alik

Reputation: 3870

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?

Upvotes: 10

Views: 3800

Answers (4)

carlitos
carlitos

Reputation: 76

In my case was the rails tag of the form, it's not @Class_form, it's <% form_tag posts_path, :method => :get %>

Upvotes: 0

datnt
datnt

Reputation: 374

Thank you Nick Zadrozny,

Our team debate today because of this issue.

The root cause of issue is because of we added Active admin.

We had to change all ".search" into ".solr_search"

Upvotes: 1

Nick Zadrozny
Nick Zadrozny

Reputation: 7944

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.

Upvotes: 18

amunds
amunds

Reputation: 702

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.

Upvotes: 38

Related Questions