Search code examples
mysqlruby-on-railsrails-activerecordgrape-api

Rails .where query not working


I have a table called Toys that has the columns toy_id, company_id, store_id, name and description

I am trying to do a query on the table like the following: toys = @company.toys.where("store_id = ? AND (name LIKE ? OR description LIKE ?)", params[:store_id], params[:query], params[:query]

The idea is if the user goes to the following url: https://myapp.com/api/toys/search?store_id=8&query="robot" that it will return all the toys for the store denoted by store_id that have a name or description LIKE robot. Each time it returns empty JSON. Not exactly sure why? Any help or guidance would be appreciated.


Solution

  • Refer to the first comment in the original question by @PardeepDhingra

    toys = @company.toys.where("store_id = ? AND (name LIKE ? OR description LIKE ?)", params[:store_id], "%#{params[:query]}%", "%#{params[:query]}%"