Search code examples
ruby-on-railsrubyransack

Ransack query on multiple fields with curl


Using the Ransack Rubygem, I am trying to make a query against two conditions using a curl. This for searching ability being added to an API.

Query on a single field

curl -X GET -G 'http://localhost:3000/api/v2/products' -d 'q[barcode_eq]=7610200237576'

This works

Processing by Api::V2::ProductsController#index as */*
  Parameters: {"q"=>{"barcode_eq"=>"761063205021"}}
  Product Load (0.4ms)  SELECT  "products".* FROM "products" WHERE     "products"."barcode" = '761063205021' LIMIT 50 OFFSET 0

Query on two fields

 curl -X GET -G 'http://localhost:3000/api/v2/products' -d 'q[barcode_eq]=7610200237576' -d 'q[barcode_eq]=7616800205113'

Ignores the first query

 Started GET "/api/v2/products?q[barcode_eq]=7610200237576&q[barcode_eq]=7616800205113" for ::1 at 2017-01-10 15:34:28 +0100
  Processing by Api::V2::ProductsController#index as */*
  Parameters: {"q"=>{"barcode_eq"=>"7616800205113"}}
  Product Load (0.6ms)  SELECT  "products".* FROM "products" WHERE "products"."barcode" = '7616800205113' LIMIT 50 OFFSET 0

What is the correct sentence to search (and / or) against multiple fields using curl or via ajax.


Solution

  • I would use the *_in matcher:

     curl -X GET -G 'http://localhost:3000/api/v2/products' -d 'q[barcode_in][]=7610200237576' -d 'q[barcode_in][]=7616800205113'
    

    Note the _in instead of the _eq and the empty brackets ([])