My Product model has a jsonb field specs
(which we\'re managing using ActiveRecord\'s store_accessor
). Many of my products\' specs have a spec in t
What you want to use is the @> operator, which tests whether your left-hand value contains the right-hand value. "Contains" works for both objects and arrays, so the following query would work:
SELECT * FROM products WHERE specs->'spec_options' @> '["spec1", "spec2"]';
Which I believe you can transform into ActiveRecord-compatible syntax like so:
scope :with_spec_options, ->(spec_options) {
where("specs->'spec_option' @> ?", spec_options.to_json)
}