Reputation: 147
I want to use search with optional params (Thinking Sphinx).
In my controller I have 3 params:
params[:model_id], params[:engine_id], params[:city_id]
Controller
def search
@search = Car.search :conditions => {
:model_id => params[:model_id],
:engine_id => params[:engine_id],
:city_id => params[:city_id]
}
end
Model
define_index do
indexes model_id
indexes city_id
indexes area_id
indexes engine_id
indexes mileage
end
When one of parameters is nil, I get an error:
Extracted source (around line #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:
My Form
= 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"
Upvotes: 1
Views: 372
Reputation: 147
define_index do
indexes name
has mileage
end
Car.search :with => {:mileage => 100..150000}
Worked
Upvotes: 0
Reputation: 2613
While searching, when you provide :conditions
, ThinkingSphinx appends @filedname in your query to scope search to that field. And sphinx does not deal with nil values
. So you should check for nil values before passing it to the search()
method.
Please keep in mind that Sphinx does not support SQL comparison operators – it has its own query language. The :conditions option must be a hash, with each key a field and each value a string.
Upvotes: 1