ActiveRecordExtended
ActiveRecordExtended copied to clipboard
Error when trying to query a JSONB field that is an array of objects
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", ...}]