Search code examples
searchruby-on-rails-3.2thinking-sphinx

Thinking Sphinx search through constant value


I am using rails 3.2 and thinking_sphinx gem and have following code in my model:

#encoding: utf-8
class Feedback < ActiveRecord::Base

  attr_accessible :feedback_type

  FEEDBACK_TYPE = { 1 => "found cheaper", 2 => "complaints", 3 => "wishes",
                   4 => "other" }

  define_index do
    indexes feedback_type
    has created_at, updated_at
    set_property delta: true
  end
end

and in my feedback_controller I have:

  @feedbacks = Feedback.search(params[:search], page: params[:page], 
                       per_page: 10,
                       sort_mode: :extended)

feedback_type is an integer type field in database and it has values from 1 to 4 according to FEEDBACK_TYPE values. In view I am showing feedback type with Feedback::FEEDBACK_TYPE[feedback.feedback_type]. Can I implement search in my model by FEEDBACK_TYPE string values typing for example "wishes" in search form? Thanks for any help.


Solution

  • Did not actually try this but following should work:

        define_index do
            indexes "case feedback_table_name.feedback_type 
                     when 1 then 'found cheaper'
                     when 2 then 'complaints'
                     when 3 then 'wishes'
                     when 4 then 'other'
                     end",  :as=>:feedback_type
            has created_at, updated_at
            set_property delta: true
        end