【问题标题】:find_with_ferret , multiple model not workingfind_with_ferret ,多个模型不工作
【发布时间】:2010-04-13 09:44:57
【问题描述】:

我有 2 个模型 A 和 B。

class A < ActiveRecord::Base
  has_one :b

acts_as_ferret :fields => [:title,:description]

在 a_cotroller 中,我写道:

@search=A.find_with_ferret(params[:st][:text_search],:limit => :all).paginate :per_page =>10, :page=>params[:page]

上面的标题和描述搜索正常工作。

B 类

现在,我想使用 3 个字段执行文本搜索;标题、描述(A 的一部分)和评论(B 的一部分)。我想在其中包含评论字段以执行雪貂搜索。然后,需要进行哪些其他更改。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    find_with_ferret 的文档表明您只需编码:store_class_name =&gt; :true 即可启用对多个模型的搜索。虽然这是真的,但还有更多内容。要搜索多个,请执行以下操作:

    @search = A.find_with_ferret(
      params[:st][:text_search],
      :limit => :all,
      :multi => [B]
    ).paginate :per_page =>10, :page=>params[:page]
    

    注意multi 选项。这是要搜索的附加索引的数组。

    现在要让他工作,您必须在将:store_class_name =&gt; :true 添加到索引定义后重建索引。

    class A < ActiveRecord::Base
      has_one :b
    
      acts_as_ferret :store_class_name => :true, :fields => [:title, :description]
    end
    

    或者...

    您可以简单地在索引定义中包含 Bs 字段:

    class A < ActiveRecord::Base
      has_one :b
    
      acts_as_ferret :fields => [:title, :description],
                     :additional_fields => [:b_content, :b_title]
    
      def b_content
        b.content
      end
    
      def b_title
        b.title
      end
    end
    

    这使一切变得简单,但不允许独立于 A 搜索 B 模型。

    【讨论】:

    • A 类 [:title, :description], :additional_fields => [:b_content, :b_title] def b_content b.content end def b_title b.title end end 它现在正在工作...谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    相关资源
    最近更新 更多