Reputation: 318
I'm attempting to apply conditional params to a thinking sphinx search.
This is what im trying to achieve (psuedo(ish) code).
I realise this is probably a dumb question or that i'm going about it in the wrong way. But any help would be great.
DistributerGame.search :conditions => {:game_genre => @genre *** IF PARAMS[GENRE] EXISTS ***}, :with => {:distributer_id => @distributer_id *** if PARAMS[DISTRIBUTER] EXISTS ***, :price_in_gbp => @price *** if PARAMS[PRICE] EXISTS ***}
Thanks, Matt
Upvotes: 2
Views: 216
Reputation: 54882
You could do something like this:
conditions = {}
conditions[:game_genre] = @genre if params[:genre].present?
withs = {}
withs[:distibuter_id] = @distributer_id if params[:distibuter].present?
withs[:price_in_gbp] = @price if params[:price].present?
# ...
DistributerGame.search :conditions => conditions, :with => withs
To persevere your params
over different pages:
#in your view:
filter_params = {}
filter_params[:game_genre] = params[:genre] if params[:genre].present?
filter_params[:distibuter] = params[:distibuter] if params[:distibuter].present?
filter_params[:price_in_gbp] = params[:price] if params[:price].present?
@distributers.each do |d|
filters_params[:distibuter] = d.id
link_to d.name, filter_params # you may have to merge this with your resource_path
@genres.each do |g|
filter_params[:genre] = g.id
link_to g.name, filter_params
Upvotes: 1