1

我想使用带有可选参数的搜索(Thinking Sphinx)。

在我的控制器中,我有 3 个参数:

params[:model_id], params[:engine_id], params[:city_id]

控制器

  def search  
    @search = Car.search :conditions => {
      :model_id => params[:model_id],
      :engine_id => params[:engine_id],
      :city_id => params[:city_id]
      }
  end

模型

define_index do
    indexes model_id
    indexes city_id
    indexes area_id
    indexes engine_id
    indexes mileage
  end

当其中一个参数为 nil 时,我收到错误消息:

提取的源代码(第 4 行附近):

1: Result:
2: %br
3: %br
4:   = render :partial => "search_item", :collection => @search, :as => :item

index car_core: syntax error, unexpected TOK_FIELDLIMIT near '  @engine_id  @city_id'

UPD1:

我的表格

= form_tag search_adverts_path, :method => :get, :id => 'admin_quick_filter' do
  %label
    = t('form.mileage')
  = text_field_tag :min_mileage, params[:min_mileage]
  = text_field_tag :max_mileage, params[:max_mileage]
  %br
  = select_tag :model_id, options_for_select(Model.all.collect{|c| [c.name, c.id]} , params[:model_id]), :include_blank => true
  %br
  = select_tag :engine_id, options_for_select(Engine.all.collect{|c| [c.name, c.id]} , params[:engine_id]), :include_blank => true
  %br
  = select_tag :city_id, options_for_select(City.all.collect{|c| [c.name, c.id]} , params[:city_id]), :include_blank => true
  %br
  %br
  = submit_tag t('form.search')


= render "search_result"
4

2 回答 2

1

在搜索时,当您提供时:conditions,ThinkingSphinx 会在您的查询中附加 @filedname 以将搜索范围扩展到该字段。而狮身人面像不处理nil values。所以你应该在将它传递给方法之前检查 nil 值search()

请记住,Sphinx 不支持 SQL 比较运算符——它有自己的查询语言。:conditions 选项必须是一个散列,每个键是一个字段,每个值是一个字符串

于 2012-12-28T13:31:49.153 回答
0
define_index do
  indexes name

  has mileage
end

Car.search :with => {:mileage => 100..150000}

工作过

于 2012-12-29T07:28:46.287 回答