You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ActiveRecord::QueryMethods::JsonbChain#path converts a nil to a string value ... which results in a value of ''. This generates the following sql for the example above:
SELECT "jsonbs"."name" FROM "jsonbs" WHERE "jsonbs"."tags"#>>'{p}' = '';
I think there are a few different ways to handle this depending on what the expected behavior of path with a nil value should be:
should it only match when value is null in JSON or,
should it match when value is null AND when the path doesn't exists
If ONLY when value is null:
the case stmt in ActiveRecord::QueryMethods::JsonbChain#path can be updated to:
casevalwhenHashop="#>"val= ::ActiveSupport::JSON.encode(val)whenArrayop="#>>"val=val.map(&:to_s)# only null value in jsonwhenNilClassop="#>"val="null"elseop="#>>"val=val.to_send
If when value is null AND when the path doesn't exists
the case stmt in ActiveRecord::QueryMethods::JsonbChain#path can be updated to:
casevalwhenHashop="#>"val= ::ActiveSupport::JSON.encode(val)whenArrayop="#>>"val=val.map(&:to_s)# null value and missing key in jsonwhenNilClassop="#>>"elseop="#>>"val=val.to_send
The text was updated successfully, but these errors were encountered:
The presence of nil is not the same as the absence of the key. For the absence, we need something like store(:tags).not.path(:a, :b) (doesn't work this way right now).
Tell us about your environment
Ruby Version: ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-darwin19]
Rails Version: rails (6.1.4.1)
PostgreSQL Version: 11.13
PgRel Version: pgrel (0.3.1)
What did you do?
Assuming:
What did you expect to happen?
I expected it to return:
["p_nil"]
What actually happened?
It actually returned:
["p_empty"]
ActiveRecord::QueryMethods::JsonbChain#path
converts anil
to a string value ... which results in a value of''
. This generates the following sql for the example above:I think there are a few different ways to handle this depending on what the expected behavior of path with a nil value should be:
If ONLY when value is null:
the case stmt in
ActiveRecord::QueryMethods::JsonbChain#path
can be updated to:If when value is null AND when the path doesn't exists
the case stmt in
ActiveRecord::QueryMethods::JsonbChain#path
can be updated to:The text was updated successfully, but these errors were encountered: