thinking-sphinx: ActiveRecord default_scope ignored

I’ve a Request class:

class Request < ActiveRecord::Base
    default_scope -> { where('user_id IS NOT NULL') }
    ...
end

When I search with Thinking Sphinx 3.1.0 Request.search I get all requests, ignoring the default scope.

Is there a way to consider the default scope?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 41 (16 by maintainers)

Most upvoted comments

Hi @hafsabatool7 - as noted in an early comment in this thread default ActiveRecord scopes aren’t used in search calls because searches aren’t SQL calls to your database - they’re instead SphinxQL calls to Sphinx.

There is the option of having Sphinx scopes - however, these apply only on single-model searches. Your example is searching across many models, and thus there’s no central source for scopes - a search on many models is a single query to Sphinx, not separate queries per model, so combining the scopes from different models into the single query is not possible.