Petran
Petran

Reputation: 8057

Check for empty hash inside where clause in rails

I want to get all books that have nil empty string or empty hash on a property

Book.where(references: [nil, "", {}])

This gives me an error.

ERROR: invalid input syntax for type json LINE 1: ...on_id" = $1 AND ("books"."references" IN ('', '{}') ... ^ DETAIL: The input string ended unexpectedly. CONTEXT: JSON data, line 1:

references is a json field on a postgres database

Upvotes: 1

Views: 1086

Answers (1)

Petran
Petran

Reputation: 8057

I have found here How to query a json column for empty objects?

Book.where(references: [nil, ""]).where("references::text = '{}'::text")

Upvotes: 2

Related Questions