Problem
Find all records in sample data where foo->bar include at least one item from given array e.g. [1,2]
Sample data
Record 1 => 'foo': {
'bar': [1,2]
}
Record 2 => 'foo': {
'bar': [3,4]
}
Record 3 => 'foo': {
'bar': [5,7]
}
Record 4 => 'foo': {
'bar': [1]
}
Record 5 => 'foo': {
'bar': [2,3]
}
Expected result
Record 1 => 'foo': {
'bar': [1,2]
}
Record 4 => 'foo': {
'bar': [1]
}
Record 5 => 'foo': {
'bar': [2,3]
}
I tried using operators @> and ?|, the first checks against JSOB and returns only if all items are included. Second has issues with types JSOB => Integer[]
SQL
SELECT "some_table".* FROM "some_table" WHERE (foo->'bar' @> '[1,2]'::jsonb);
Rails Scope
scope :for_bar, -> (bars) { where("foo->'bar' @> ?::jsonb", bars.to_json) }
Any suggestions to solve this.