Search code examples
ruby-on-railsrubypostgresqljsonb

Rails - Postgres - jsonb column query


I have a jsonb column named 'available_quantity'. which will have sample values

{ "100": 50, "1000":10 }

Now, I want to query all keys with values less than 50.

I tried this query,

Bundle.where('available_quantity @> ?', {'100': 5}.to_json)

But this one gives me all the Bundle with available_quantity containing {100: 5}.

How can I do that? Is that even possible?


Solution

  • You can use the ->> operator:

    Bundle.where("(available_quantity->>'100')::int < 50")