Thinking Sphinx? Watch your scopes!
20/08/2010
I ran into this little GOTCHA, and thought it could use some more Google love, so here goes:
If you try to use named scopes with your Sphinx search, they will just be ignored. For example, I had a model JobOffer with this scope:
scope :open, { :conditions => 'job_offers.status = "open" OR job_offers.status = ""' }And then did a search like this:
@job_offers = JobOffer.open.search query
Which, to my surprise was also showing "closed" job offers.
I found the solution over here; Sphinx has (since version 1.2) it's own form of scopes.
sphinx_scope(:open){
{ :conditions => {:status => ['open', '']}}
}
With that sphinx_scope in place, the above search call wìll work as expected. (well, obviously I've had to rename it to not clash with the existing Rails scope.)