gpt4 book ai didi

ruby-on-rails - Thinking Sphinx, Rails, :has_many :through => . .. 有

转载 作者:太空宇宙 更新时间:2023-11-03 16:11:51 25 4
gpt4 key购买 nike

模型:产品

has-many product-categories, :through => ...

问题 1)如何索引多对多与 thinking sphinx 的关联

我必须使用 has 吗?

问题 2)如何在 Controller 中搜索 this

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

最佳答案

我还没有在 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

在 Controller 中:

@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

在 View 中:

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

如果您还没有它,我强烈推荐 peepcode 上的 thinking-sphinx 书:https://peepcode.com/products/thinking-sphinx-pdf

希望对您有所帮助。

关于ruby-on-rails - Thinking Sphinx, Rails, :has_many :through => . .. 有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1945661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com