Suganya Selvarajan
Suganya Selvarajan

Reputation: 1082

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?

Upvotes: 1

Views: 248

Answers (1)

Sebastián Palma
Sebastián Palma

Reputation: 33420

You can use the ->> operator:

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

Upvotes: 1

Related Questions