rails-sqlserver / activerecord-sqlserver-adapter

SQL Server Adapter For Rails
MIT License
972 stars 558 forks source link

How to query a JSON field for individual contained fields? #1030

Closed spacerobotTR closed 1 year ago

spacerobotTR commented 1 year ago

Using Rails 7, Active record sql server adapter and SQL server 2016. I have a model called requests which has a field called properties where I am storing a json list of fields and values. Here is how the field value looks:

{"summary":"Need bed raised","optional_information":"too low","location":"26679","reported_phone":"(555) 334-1324","reported_email":"mail@mail.com","affected_person_id":"1231232","affected_person_name":"testuserJ ","affected_person_phone":"(555) 334-1324","affected_person_email":"mail@mail.com"}

I am trying to query based on rails params like this:

@request = current_user.requests.where("properties->'summary' LIKE ?", "%#{params[:filter]}%") if params[:filter].present?

I am getting a syntax error TinyTds::Error: Incorrect syntax near '>'.

Does anyone know how to query these sort of fields with a like wildcard?

aidanharan commented 1 year ago

What does the raw SQL that you want to run against the SQL Server database look like?