I am using jsonb datatype for a column named data. When I query using a simple hash, it works correctly:
[1] pry(PredictionService)> Prediction.where(data: {"innings_no" => 1})
Prediction Load (1.2ms) SELECT "predictions".* FROM "predictions" WHERE "data"."innings_no" = 1
=> #<Prediction::ActiveRecord_Relation:0x3fcb34634e78>
It fails with an incorrect SQL when I use an array like this:
[2] pry(PredictionService)> Prediction.where(data: {"innings_no" => [1,2]})
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "data" does not exist
LINE 5: WHERE a.attrelid = '"data"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"data"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
from /Users/lenin.rajasekaran/.rvm/gems/ruby-2.3.1@duggout-app/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'
Is this a known issue with jsonb/ActiveRecord or this can be fixed?
I cannot use Postgres's array functions to access a particular key, as the keys are dynamic and I am using this query to find existing records before creating a new one.
Prediction.where(data: {"innings_no" => 1}).to_aand it gave me the expected results" sure?datais a table name andinnings_nois a column in that table. I thought it is working because I saw a valid#<Prediction::ActiveRecord_Relation:0x3fcb34634e78>resultdatalook like? Is it a JSON array, object, something else?Hashobject. ActiveRecord acceptsHashfor thedatacolumn while increatemethod. But does not accept the same when using it inwhereclause.