2013-01-06 1 views
0

정렬 :타이어 문제와 나는 타이어 보석을 사용하여 분류 검색을 할 노력하고있어하지만 난 항상이 오류 얻을 검색

구문 분석 실패 [에 정렬하려면 [직업]에 대한 찾을 수 없습니다 매핑]

내 코드에 어떤 문제가 있습니까? 다음은 내 사용자 모델

class User 
    include Mongoid::Document 
    include Mongoid::Timestamps 
    include Tire::Model::Search 
    include Tire::Model::Callbacks 

    belongs_to :occupation 

    field :name, :type => String, :default => "" 
    field :email, :type => String, :default => "" 
    field :kind, :type => String, :default => "" 

    def to_indexed_json 
    { id: id, 
     name: name, 
     email: email, 
     kind: kind, 
     occupation: occupation.try(:name), 
     created_at: created_at, 
     updated_at: updated_at 
    }.to_json 
    end 

    mapping do 
    indexes :id, :type => 'integer' 
    indexes :name, :type => 'String' 
    indexes :email, :type => 'String' 
    indexes :kind, :type => 'String' 
    indexes :occupation, :type => 'String' 
    indexes :created_at, :type => 'Date' 
    indexes :updated_at, :type => 'Date' 
    end 

    def self.search q, params={} 
    tire.search page: (params[:page] || 1) do |search| 
     search.query do |query| 
     query.string q 
     end 
     search.sort do |sort| 
     sort_column = params[:sort] || 'occupation' 
     sort_direction = params[:direction] || 'asc' 
     sort.by(sort_column.to_sym, sort_direction) 
     end 
    end 
    end 
end 

답변

0

시도해 볼 수 있습니까? 나는 당신이 to_indexed_json에 occupation.name을 보내고 있고 당신이 전체 직업 모델을 정렬하려고 시도하고 있다고 생각합니다.

class User 
    include Mongoid::Document 
    include Mongoid::Timestamps 
    include Tire::Model::Search 
    include Tire::Model::Callbacks 

    belongs_to :occupation 

    field :name, :type => String, :default => "" 
    field :email, :type => String, :default => "" 
    field :kind, :type => String, :default => "" 

    def to_indexed_json 
    { id: id, 
     name: name, 
     email: email, 
     kind: kind, 
     occupation: occupation.try(:name), 
     created_at: created_at, 
     updated_at: updated_at 
    }.to_json 
    end 

    mapping do 
    indexes :id, :type => 'integer' 
    indexes :name, :type => 'String' 
    indexes :email, :type => 'String' 
    indexes :kind, :type => 'String' 
    indexes :occupation, :type => 'object' do 
     indexes :name, :type => 'String' 
    end 
    indexes :created_at, :type => 'Date' 
    indexes :updated_at, :type => 'Date' 
    end 

    def self.search q, params={} 
    tire.search page: (params[:page] || 1) do |search| 
     search.query do |query| 
     query.string q 
     end 
     search.sort do |sort| 
     sort_column = params[:sort] || 'occupation.name' 
     sort_direction = params[:direction] || 'asc' 
     sort.by(sort_column, sort_direction) 
     end 
    end 
    end 
end