Colibri
Colibri

Reputation: 1195

How to correctly implement where for jsonb array?

I added the jsonb field in which I want to store the ID.

Next, I need to implement a "search" of records by the contents of this field.

Everywhere, including the documentation (link), I saw this operator - @>. But I didn't manage to use it:

User.where.not("migration_ids @> ?", 1)
ActiveRecord::StatementInvalid (PG::UndefinedFunction: ERROR:  operator does not exist: jsonb @> integer)
LINE 1: ...s".* FROM "users" WHERE NOT (migration_ids @> 1) LIMI...
                                                      ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

Tell me, please, how to do it right?

Upvotes: 0

Views: 52

Answers (1)

Kvr Ramya
Kvr Ramya

Reputation: 36

This should work User.where.not("migration_ids @> ?", "1")

Upvotes: 1

Related Questions