0
연령대별로 검색을 필터링하고 싶습니다. 이제탄성 검색 : 타이어 보석이있는 그룹 별 필터링
내가 가진 : program.rb
class Program < ActiveRecord::Base
has_many :age_groups, through: :programs_age_groups_relations
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :name, analyzer: 'snowball', boost: 100
indexes :description, analyzer: 'snowball'
indexes :age_groups do
indexes :name, analyzer: 'snowball'
end
end
def to_indexed_json
to_json(include: {age_groups: {only: :name}})
end
def self.search(params, options={})
tire.search(load: {include: 'age_groups'}) do
query do
boolean do
must { string params[:name_query] } if params[:name_query].present?
end
end
filter do
boolean do
if params[:age_groups].present?
params[:age_groups].each do |ag_name|
must { string ag_name }
end
end
end
end
end
end
index.html.haml
컨트롤러= form_tag programs_path, method: :get do |f|
= text_field_tag :name_query, params[:name_query]
- AgeGroup.all.each do |ag|
= check_box_tag 'age_groups[]', ag.name, params[:age_groups].include?(ag.name)
:
@programs = Program.search(params)
연령 그룹 :
AgeGroup.create([{name: 'Baby'}, {name: 'Toddler'}, {name: 'Preschoolers'}, {name: 'Elementary'}, {name: 'Middle School'}])
Realtion :
class ProgramsAgeGroupsRelation < ActiveRecord::Base
attr_accessible :age_group_id, :program_id
belongs_to :age_group
belongs_to :program
end
내가 검색 양식 아무것도에서 하나 이상의 age_groups을 확인 검색 결과로 발생했다.
어떻게이 작업에 타이어를 올바르게 사용할 수 있습니까?