2009-12-22 60 views

回答

2

我没有在has_many上试过这个:通过这样在火焰中击中我,如果有的话,但我不明白为什么这对你也不适用(我在has_many关联中使用它),你基本上在索引定义中使用你的关联。然后针对该模型的搜索也将搜索子记录。

class Product < ActiveRecord::Base 
    has_many :product_categories 

    define_index do 
    indexes a_product_field_to_index 
    indexes product_categories.name, :as => :categories 
    end 
end 

在控制器:

@products = Product.search(params[:query] || '') 
#params[:query] is simply the search string, I can't remember if you need to sanitize this, I would always assume you do unless you find out otherwise 

在视图:

@products.each do |p| 
    p.categories.each do |cat| 
    end 
end 

如果您还没有它,我会强烈建议可用的思维 - 狮身人面像书上peepcode: https://peepcode.com/products/thinking-sphinx-pdf

希望有所帮助。