Skip to content

Error when trying to query a JSONB field that is an array of objects #100

Description

@avanrielly

I am not sure if there is something wrong with my setup or if there is an issue with the gem. It seems that I cannot use the contains query against JSONB fields that contain an array of objects. It returns an error trying to parse the parameters. The raw Postgres query works fine.

Raw PostgresSQL query

Button.where("draft_button_fields @> ?", [{required: "always"}].to_json).count
  Button Count (1.7ms)  SELECT COUNT(*) FROM "buttons" WHERE (draft_button_fields @> '[{"required":"always"}]') /*line:(pry):2:in `__pry__'*/
=> 1

It errors with or without the array.
Error:

Button.where.contains(draft_button_fields: [{required: "always"}]).to_sql
TypeError: can't quote Hash
from /Users/avanrielly/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/activerecord-7.0.8.1/lib/active_record/connection_adapters/abstract/quoting.rb:25:in `quote'

Table structure:

                                    Table "public.buttons"
         Column         |              Type              | Collation | Nullable |   Default
------------------------+--------------------------------+-----------+----------+-------------
 id                     | bytea                          |           | not null |
...
 draft_button_fields    | jsonb                          |           |          | '[]'::jsonb

Column data:

[{"field_id": "name", "required": "always", ...}]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions