我正在使用rails 3.2和thinking_sphinx gem,并在我的模型中使用以下代码:
#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
在我的feedback_controller中我有:
@feedbacks = Feedback.search(params[:search], page: params[:page],
per_page: 10,
sort_mode: :extended)
feedback_type是数据库中的整数类型字段,根据FEEDBACK_TYPE值,其值为1到4。在视图中我显示反馈类型与Feedback :: FEEDBACK_TYPE [feedback.feedback_type]。我可以通过FEEDBACK_TYPE字符串值在我的模型中实现搜索,例如在搜索表单中键入“祝福”吗?谢谢你的帮助。
答案 0 :(得分:1)
实际上没有尝试过,但是以下应该可以工作:
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
答案 1 :(得分:0)
你也可以写得更强大,比如:
FEEDBACK_TYPE = { 1 => "found cheaper", 2 => "complaints", 3 => "wishes",
4 => "other" }
kind_of_types = lambda do
str = 'case feedback_table_name. feedback_type '
FEEDBACK_TYPE.each { |type| str += "when #{type[0]} then '#{type[1]}' " }
str += 'end'
end
define_index do
indexes kind_of_types, as: :feedback_type
has created_at, updated_at
set_property delta: true
end