0

I was following Ryan Bates screencast and building a basic search, but decided I needed something better than plain MySQL text search and was hoping the Sol Sunspot gem might work.

As per the screencast http://media.railscasts.com/videos/037_simple_search_form.mov, I have setup my search within my model.

My controller has a simple

def index
  @tasks = Task.search(params)
end

And my model has

def self.search(params)
   if params[:search]
      search do
           keywords params[:search]
           paginate :page=>params[:page], :per_page =>20
      end
   else
     select('id,title,desc').paginate(:page=>params[:page], :per_page =>20)
   end
end

When I load the page without a seach, I get the correct output.

When I provide a search term, I'm getting

wrong number of arguments (0 for 1)

I've tried both 'search do', and 'Task.search do', as that seems to be the difference between the tutorials I've seen http://tech.favoritemedium.com/2010/01/full-text-search-in-rails-with-sunspot.html, and the way I'm doing it, being passed from the controller into a method.

Any suggestions on how to get this going??

2 Answers 2

1

As workergnome mentions, the precise reason for the error is that you've already defined a search method, which you're trying to call, from within itself, without the expected params.

What you need to call is the solr_search method, which is the actual method that Sunspot defines (named thus precisely to allow you to provide your own custom search method).

Furthermore, the object returned by the solr_search method is a Search object with some meta information about the search itself. You likely want the instantiated ActiveRecord objects returned by the results method on that object.

Putting those together, here's what you want your search method to end up looking like:

def self.search(params)
  if params[:search]
    solr_search do
      keywords params[:search]
      paginate :page => params[:page], :per_page => 20
    end.results
  else
    select('id,title,desc').paginate(:page => params[:page], :per_page => 20)
  end
end
Sign up to request clarification or add additional context in comments.

Comments

0

You're getting the "wrong number of arguments" because you've defined self.search as a function on the model, and then you're trying to reference the Sunspot search method.

Have you tried skipping using the Rails adaptors, and going directly with the Sunspot syntax?

Sunspot.search(self) do
  keywords params[:search]
  paginate :page=>params[:page], :per_page =>20
end

Might resolve your issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.