pixelearth
pixelearth

Reputation: 14630

is there a way to mix rails named scopes and thinking sphinx scopes?

Is there a way to mix named scopes and sphinx scopes? I can't believe there's not.

I have sphinx index on Lessons. Once users purchase lessons, they have a screen that they can see a list of all of their lessons. I want them to perform the same searches they can on site-wide lessons, but scoped to the ones they own.

This is obviously not possible with sphinx-only scopes, because I'm not indexing the purchasing data, nor do I think I should have to.

Is there any way to accomplish this?

This is what I have so far to "combine" the two systems. It seems hackish to me but it works:

lesson_ids = current_user.active_products_by_type(:lessons).collect{|x| x.id}
@lessons = Lesson.active_scope.search :with => {:id => lesson_ids }

Upvotes: 6

Views: 1144

Answers (2)

Dennis Krupenik
Dennis Krupenik

Reputation: 676

I guess it's better to end with AR scopes to be able to do .includes, .joins etc:

sphinx_scope(:visible) do
  { with: {is_visible: true} }
end

@items = Item.where(
  id: Item.visible.search_for_ids(nil,
    page: params[:page],
    with: {
      tag_id: @tag.id,
      category_id: current_category.id
    }
  )
).order(:id).includes(:photos)

Upvotes: 2

pat
pat

Reputation: 16226

It's a bit hard to tell whether your work-around is appropriate, given I'm not sure what the scopes active_products_by_type and active_scope are (which is the Sphinx one? Both? Neither?).

But you cannot call an AR scope on a search call, because AR scopes are built under the hood to generate SQL queries. Sphinx has its own querying protocol, which is why Thinking Sphinx added Sphinx scopes. Added to that is that Sphinx can't reference the database while searching (only while indexing), so any reference to data that Sphinx can't see is not going to be helpful either.

All that said, there's probably nothing in the code that will let you chain a search call onto an AR scope - and you will likely even get results back - but this is because the scope affects the Model.find call that Thinking Sphinx makes to convert search results into model instances. The upshot is that while you may see objects, the AR scope is only being applied to the given page of search results, and the pagination math (number of pages/total results) will be unreliable.

Urgh, that turned into a bit of an essay, but hopefully it helps...

Upvotes: 4

Related Questions