Juan Sebastian Totero
Juan Sebastian Totero

Reputation: 505

Rails + Thinking Sphinx : search condition over array

I'm looking for a way to do the following. Actually, I'm searching some articles and when launching the search I'm giving as a parameter an array, i.e. `params[:categories] = ["1","4","5","8"]

Now, when searching with thinkingSphinx `I do the following

#article.rb
def self.adv_search(query, categories)
    Article.search(
      query, 
      :with => {:category =>  },
    )
end

and launching the search like

adv_search(params[:q], params[:categories])

but I keep obtaining an empty results array. Anyone knows how to TS manage arrays?

Upvotes: 0

Views: 1611

Answers (1)

pat
pat

Reputation: 16226

If category is an attribute which is either an integer or collection of integers (going by your example), then the one thing you'll need to do is make sure you're feeding an array of integers, not strings, into the filter:

Article.search query, :with => {:category => categories.collect(&:to_i)}

Keep in mind that this will return all articles with any of those categories. If category was a collection of integers and you wanted articles that had all of those categories, then you should use :with_all instead of :with.

Upvotes: 3

Related Questions