Searchkick: Select All data for a specific field when doing Where statement

Created on 25 Mar 2016  路  6Comments  路  Source: ankane/searchkick

Hello,

I have a situation with the multiform search and I want to implement the solution with the usage of select boxes and checkboxes. For a better representation - I have these search fields (for autocommerce website):

Mark (selectbox1) // Model (selectbox2)
Price from (selectbox3) // Price to (selectbox5)

In ruby (on rails) I'm doing something like this:

    @vehicles = Vehicle.search where: {
      mark:  params[:mark],
      model: params[:model]
      price: params[price_from]..params[:price_to]
    }

Question: Supposedly user just selects _price range_ - how do I still query all other fields with the "select all" query? But other times user will select just the mark and the model and I want to query all prices.. What is the select all operator for a specific field where rules for other fields still apply?

So.. how do I do something like this:

    @vehicles = Vehicle.search where: {
      mark:  params[:mark].blank? ? params[mark] : select_all_from_mark
      model: params[:model].blank? ? params[:model] : select_all_from_mark
      price: params[price_from]..params[:price_to]
    }

Thanks, couldn't make it work.

Most helpful comment

One way is:

where = {price: params[price_from]..params[:price_to]}
where[:mark] = params[:mark] if params[:mark]
where[:model] = params[:model] if params[:model]
@vehicles = Vehicle.search where: where

All 6 comments

One way is:

where = {price: params[price_from]..params[:price_to]}
where[:mark] = params[:mark] if params[:mark]
where[:model] = params[:model] if params[:model]
@vehicles = Vehicle.search where: where

Ankane thanks for the reply.

This doesn't seem to work.

Must be something else with your set up. I use this pattern frequently.

I get a Syntax error with this:

where = {price: params[:price_from]..params[:price_to]}
where[:mark] = params[:mark] if params[:mark]
where[:model] = params[:model] if params[:model]
@vehicles = Vehicle.search where: where

bump

Cleaning up issues

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rajatgarg79 picture rajatgarg79  路  4Comments

neyp picture neyp  路  3Comments

namila picture namila  路  4Comments

gerrywastaken picture gerrywastaken  路  3Comments

cubaraphael picture cubaraphael  路  3Comments