2016-10-06 4 views
1

Userモデルがありますが、これはSkillsですが、Masteriesです。入れ子モデルのAlgolia_search

私はAlgoliasearchを使用してユーザーを取得するためにフォームを使用しています。特定のスキルを持つすべてのユーザーを取得したいと考えています(Internet Explorer、「Origami」スキルを持つ「John」というユーザーがいる場合、私は「ジョン」や「折り紙」を入力した場合、彼は、私は、ネストされたスキルをマッピングすることにより、そうしようとした

)結果に表示されるはずですが、これはここで

がモデル

です動作するようには思えません
class Creator < ActiveRecord::Base 
include AlgoliaSearch 


algoliasearch do 
    # all attributes will be sent 
    add_attribute :creator_skills 
end 

has_many :masteries 
has_many :skills, through: :masteries 

def creator_skills 
    self.masteries.map do |s| 
    { name: s.skill.name } 
    end 
end 

[...] 

フォームは、queryパラメータを返します。これは、イブのクリエイター:

@creators = Creator.where(display_index: true).algolia_search(params[:query]).shuffle 

私は何かを見逃しましたか?ネストされたモデルをマップすることは可能ですか?

+0

ドキュメントはそれを達成する方法について説明します。https://github.com/algolia/algoliasearch-rails#nested-objectsrelations – MrYoshiji

+0

@MrYoshiji私が試した、これは動作しないようにそれはそう'' 'has_many_though'''の関係で –

+0

この投稿はお役に立ちましたか?https://github.com/algolia/algoliasearch-rails/issues/31 – MrYoshiji

答えて

2

.where句とalgolia_searchの両方の方法を組み合わせることはできません。また、ファーミング機能を使用してスキルをフィルタリングする必要があります。それは次のようになります。

class Creator < ActiveRecord::Base 
    include AlgoliaSearch 

    algoliasearch do 
    add_attribute :creator_skills 

    attributesForFaceting ['creator_skills', 'display_index'] 
    end 

    has_many :masteries 
    has_many :skills, through: :masteries 

    def creator_skills 
    # array of skill names 
    self.skills.map(&:name) 
    end 

    [...] 

end 

# empty query (= match all) + facet filters 
Creator.algolia_search('', filters: 'creator_skills:Origami AND display_index:true') 
関連する問題