How can I query for all records with an empty array, using the Postgres array data type?
create_table "users", force: :cascade do |t|
t.string "email", limit: 255, default: "",null: false
t.string "roles", default: [], array: true
end
I want to query for all records with an empty roles array.
Tried User.where("roles @> ?", '{}')
but that did not work (returned 0 records).
Here is the syntax you can use:
User.where("roles = '{}'")
You can also do: User.where(roles: [])
I'm using Rails 5.2 (although the syntax may have been introduced earlier). I realize this is an old question, but in case anyone finds it from a Google search (like me).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With