Trying to allow for generalized array or value queries like rails uses to allow for where(a: [1]) or where(a: 1) to work and more

Viewed 23

So the data I'm searching for is something like (hundreds of them with various combinations of values):

attributes1:
  attr1: "1",
  attr2: "3",
  attr3: ["A,"B"],


attributes2:
  attr1: "2",
  attr2: "5",
  attr3: ["B,"D"],

attributes3:
  attr1: "2",
  attr2: "4",
  attr3: ["B,"D"],

I came across this:

Check whether array in JSONB column includes any of the values in another array

Which is close but not quite there (isn't working, unclear why).

A complication though, the queries are predefined and used to populate dropdown menus, so things like { attr3: "A", attr2: "4" } and { attr1: ["1", "2"]} }. Basically very generalized.

I have it working for the easier case of { attr1: "2" } yielding attributes2 and attributes3 (or attr1: '2", attr2: "4" only giving attributes3, but trying to also handle arrays both as an attribute value and as a query value now.

1 Answers

Before you use each value from an attrX: you can cast it using Array().

# Will wrap in an Array
Array("1") => ["1"]

# Will return the original Array
Array(["1"]) => ["1"]

Any method that uses that data can be written to always expect an array.

Related