Search code examples
ruby-on-rails-3thinking-sphinx

Searching through a collection of indexed model objects using Thinking Sphinx


I have a Technique model which belongs_to User and is indexed by Thinking Sphinx.

I also have a method in my model that returns an array of Technique objects:

def possible_children(user)
    user.techniques - (self.children + [self])
end

This just takes the techniques that a user has, subtracts out those of the techniques that are already the children of the 'self' technique object, along with 'self' itself, and returns the remaining technique objects.

Then in a controller I instantiate a collection of possible children like so:

 @possible_children = @technique.possible_children(current_user).search params[:search]

This returns an "undefined method 'search' for #"

Not sure if this is relevant but the controller this takes place in is not the TechniquesController.

What I am trying to do is search an arbitrary collection returned by a Model method.

Any ideas?

Let me know if I need to provide more information. Thank you.


Solution

  • I'm afraid this isn't possible with Thinking Sphinx - at least, not that simply. What you could do is use the objects you want to search across, grab their ids, and use that in a filter:

    possible_children = @technique.possible_children(current_user)
    Technique.search params[:search],
      :with => {:sphinx_internal_id => possible_children.collect(&:id)}
    

    Sphinx has its own id, but the primary key from the database is stored as the attribute sphinx_internal_id by Thinking Sphinx.