ruby - Multiple where clauses in rails searchkick -
i have basic question searchkick. if want join multiple statements in searchkick query using if statements. query-builder
@product = product.all unless request.end_date.nil? @product = @product.search, where('created_at <= ?', request.end_date) end unless request.max_price.nil? @product = @product.search, where('price <= ?', request.max_price) end @product
the above code works fine if request has either end date or max_price. if has both, throws error. there way construct or concatenate 2 statements. cannot
product.search '*', where('created_at <= ?', request.end_date), where('price <= ?', request.max_price)
because if statement important.
you should check docs of searchkick, has or filter: https://github.com/ankane/searchkick
where: { expires_at: {gt: time.now}, # lt, gte, lte available or: [ [{in_stock: true}, {backordered: true}] ] }
for case, can deal below:
conditions[:or] = [[]] unless request.end_date.nil? conditions[:or][0] += [{created_at: {lt: request.end_date}}] end unless request.max_price.nil? conditions[:or][0] += [{price: {lt: request.max_price}}] end product.search '*', where: conditions
Comments
Post a Comment