Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 615
Mitya Avatar asked Feb 03 '26 14:02

Mitya


1 Answers

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.

like image 82
pat Avatar answered Feb 06 '26 15:02

pat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!