开发者

Rails + Thinking Sphinx : search condition over array

开发者 https://www.devze.com 2023-03-15 03:15 出处:网络
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\",\"

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])
开发者_Python百科

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消