I'm working with Rails 5 + Postgres.
I have a Postgres JSONB column named data with data that looks something like:
{username: 'McGruff', timestamp: 123456789}
I would like to query for data that is between two timestamps, to get a subset of records that all have a timestamp within some range (say, the last 24 hours).
Using comments from below, the answer is:
Model.where("(data->'timestamp')::int BETWEEN ? AND ?", start, end)
Thanks for the help!
->gives you ajsonbvalue, not a string or integer;->>gives you a string back, then cast that string to an integer to get the right comparison logic:(data->>'timestamp')::int between ? and ?.