首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >思考Sphinx、Rails、:has_many :通过=> ...有

思考Sphinx、Rails、:has_many :通过=> ...有
EN

Stack Overflow用户
提问于 2009-12-22 19:01:42
回答 1查看 2.5K关注 0票数 2

型号:产品

代码语言:javascript
复制
has-many product-categories, :through => ...

问题1)如何索引多对多与思考中的狮身人面像的关联?

我必须使用has吗?

问题2)如何在控制器中进行搜索

例如。搜索参数: Product.search -params,:conditions => {some_conditions}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-12-22 19:38:11

我还没有在has_many上尝试过这一点:所以如果你尝试过,那就让我失望吧,但我不明白为什么这对你不起作用,(我在has_many关联上使用它)你基本上是在索引定义中使用你的关联。然后,对该模型的搜索也将搜索子记录。

代码语言:javascript
复制
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

在控制器中:

代码语言:javascript
复制
@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

在视图中:

代码语言:javascript
复制
@products.each do |p| 
  p.categories.each do |cat|
  end
end

如果你还没有,我强烈推荐在peepcode:https://peepcode.com/products/thinking-sphinx-pdf上提供的sphinx sphinx书籍。

希望这能有所帮助。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1945661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档