2010-04-13 82 views
0

我有2種型號A和B.find_with_ferret,多模型不能正常工作

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類<的ActiveRecord :: Base的 belongs_to的:一個

現在,我想用3個字段進行文本搜索;標題,描述(A的一部分)和評論(B的一部分)。在那裏我想包括評論領域進行雪貂search.Then,還需要其他什麼更改。

回答

0

find_with_ferret的文檔表明您只需編碼:store_class_name => :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 => :true之後重建索引。

class A < ActiveRecord::Base 
    has_one :b 

    acts_as_ferret :store_class_name => :true, :fields => [:title, :description] 
end 

或者......

你可以簡單地包括在索引定義燒烤場:

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 

這讓一切簡單,但不允許搜索的B型獨立的A.

+0

class A [:title,:description], :additional_fields => [:b_content,:b_title] DEF b_content b.content 端 DEF b_title b.title 端 端 它現在工作...謝謝。 – jissy 2010-04-20 11:52:17