I have a search form that the user can search for an Article content and narrow it down using a select box with categories. When the category param is blank, it searches for a blank id, but I’d like to return any category in that case. Is it possible to configure Thinking Sphinx to accomplish that or do I have to include this condition on my controller?
Article model:
class Article < ActiveRecord::Base
belongs_to :category
define_index do
indexes :name
indexes content
has category(:id), :as => :category_id
end
...
end
SearchController:
@articles = Article.search params[:q], :with => {:category_id => params[:category]}
For Sphinx, nils/NULLs are treated as 0’s, so if no params[:category] value is passed through to the controller, it’s best if you just don’t pass through a filter on that attribute. Something like this, perhaps: