型号:产品
has-many product-categories, :through => ...问题1)如何索引多对多与思考中的狮身人面像的关联?
我必须使用has吗?
问题2)如何在控制器中进行搜索
例如。搜索参数: Product.search -params,:conditions => {some_conditions}
发布于 2009-12-22 19:38:11
我还没有在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上提供的sphinx sphinx书籍。
希望这能有所帮助。
https://stackoverflow.com/questions/1945661
复制相似问题