10

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?

alik
  • 3,820
  • 9
  • 41
  • 55

4 Answers4

38

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.

amunds
  • 702
  • 8
  • 22
  • that definitely worked! hmmm i checked my gems and could not see what would be causing the conflict, figuring this out... any further insights will be much appreciated! – alik Oct 28 '11 at 08:19
  • It's loading MetaSearch somehow, have you checked your vendor/plugins or vendor/gems directory? You can use Gem.loaded_specs.keys to see what Gems are currently loaded. – amunds Oct 28 '11 at 08:34
  • 6
    ok i found it in the gemfile.lock. apparently the activeadmin gem uses it. maybe i should just get rid of active admin altogether if I want sunspot to work. can you recommend any gem that is similar to activeadmin http://activeadmin.info/ – alik Oct 28 '11 at 08:38
  • Have you tried adding Sunspot above ActiveAdmin in the gemfile? Also, there is no adverse effect of having Sunspot.search(Product) over Product.search. So if both Sunspot and ActiveAdmin work I wouldn't bother looking for a replacement. – amunds Oct 28 '11 at 08:43
  • 1
    I tried rearranging but it did not affect it. it seems like the gems are dependent on the gemfile.lock which seems to order gems alphabetically so I can not have Sunspot above ActiveAdmin. Yes I agree there are no adverse effect, I kind of don't like the idea of ActiveAdmin messing with the way the clean and nice way the Sunspot syntax is supposed to be. – alik Oct 28 '11 at 08:50
18

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.

Nick Zadrozny
  • 7,906
  • 33
  • 38
1

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"

datnt
  • 372
  • 2
  • 15
0

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

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
carlitos
  • 76
  • 5