2014-11-25 96 views
0

我不明白我如何使用太陽黑子來搜索has_many關聯。用戶從select_tag字段中選擇技能,結果返回與skill_ids匹配的記錄。太陽黑子搜索有很多協會

只是爲了記錄在案,當我按下搜索按鈕我得到這個回:

SOLR Request (294.1ms) [ path=select parameters={fq: ["type:User"], q: ["4"], fl: "* score", qf: "", defType: "edismax", start: 0, rows: 30} ] 

q: ["4"]提到這是我搜索的技能的ID數4。當我想要找回某些東西時,我得不到任何結果。當我提交時沒有參數,我確實讓所有用戶返回結果。

到目前爲止我所做的和想出的結果可以在下面找到。任何提示或幫助正確的方向非常感謝。

(請讓我知道如果你需要任何其他信息,我將它添加馬上)

我的結構如下:

在用戶模式(user.rb)

has_many :user_skills, dependent: :destroy 
    has_many :skills, through: :user_skills 

    searchable do 
    integer :skill_ids, :multiple => true do 
     skills.map(&:id) 
    end 
    end 

在技能模型(skill.rb)

belongs_to :user 
    has_many :user_skills 
    has_many :users, through: :user_skills 

    searchable do 
    integer :id, :multiple => true 
    end 

DB模式:

t.string "name" 
t.string "category" 
t.string "slug" 

在User_skills模型(user_skill.rb)

belongs_to :user 
    belongs_to :skill 

DB模式:

t.integer "user_id" 
t.integer "skill_id" 

在我的搜索控制器我有以下的代碼段:

@search = Sunspot.search(User) do 
    fulltext params[:s_skills] 
end 

@users = @search.results 

PARAMS [:s_skills]是從select_tag提取的skill_ids:

<%= select_tag :s_skills, options_for_select(Skill.all.collect{|e| [e.name,e.id]}, skills), {:id => 'e2', :multiple => true, :placeholder => "Input skills", class: "input-search2" } %> 

這需要技巧的名字,並返回其ID。

回答

1

你實際上並沒有在你已經索引的skill_ids字段中搜索。

您必須添加類似:

User.search do 
    fulltext params[:free_text] #<-- YOU DON'T NEED THIS, PROBABLY? 
    with(:skill_ids, params[:s_skills]) if params[:s_skills] 
end 
+0

謝謝幫助,但它確實表明我既相匹配的IDS的記錄。例如,如果我搜索Ruby和Java,那麼當我想要查看具有Ruby和Java技能的用戶時,它向我展示了具有Ruby或Java技能的用戶。有沒有辦法來調整它? – 2014-11-25 14:28:22

+0

固定。我只是使用「all_of」。太感謝了 – 2014-11-25 15:06:29