I'm building a query using ransack predicates, so that means I need a lot of conditions for a WHERE clause SQL-wise. Here's what I have as a Hash of conditions for ransanck's search
method:
This is the console output of params[:q]
{"price_gteq"=>"0", "rooms_gteq"=>"1", "baths_gteq"=>"1", "has_photos_true"=>"0", "zone_id"=>"1", "deal_type_eq"=>"1", "s"=>{"0"=>{"name"=>"price", "dir"=>"asc"}}, "price_lteq"=>"2500000", "rooms_lteq"=>"2", "baths_lteq"=>"7", "sector_id_in"=>nil}
And this is the SQL query it generates:
Property.search(params[:q]).result.to_sql
Which gives:
SELECT "properties".* FROM "properties" LEFT OUTER JOIN "sectors" ON "sectors"."id" = "properties"."sector_id" WHERE (("properties"."price" >= 0.0 AND "properties"."price" <= 2500000.0)) ORDER BY "properties"."price" ASC
How can I include ALL the fields inside the where
clause?
As you can see, only the :price
field remains inside the where
clause. I need :baths
and :rooms
to be included for the where
clause too.
How can I fix it?
I've solved my own problem fortunately, What happened was that I was using the UNRANSACKABLE_ATTRS array for my model to exclude the aforementioned fields from sorting (The default Sorting methodology provided by Ransack), but I didn't know this would affect search as well. Anyways I removed the UNRANSACKABLE_ATTRS array and every condition in search started to work as expected. :)