Rails PostgreSQL find by nested JSON / JSONb field
Today I've Learned postImaging Rails model SubscriptionsHistory
with json field data
that has nested JSON: subscription.id
So SubscriptionsHistory.attributes
would return:
{"id"=>654321,
"data"=>
{"subscription"=>
{"id"=>"169lTASJ5wfsY3y3u",
"other_field"=>"xxx"},
"other_field"=> { ....}
"created_at"=>Mon, 14 Dec 2020 02:00:19 UTC +00:00,
"updated_at"=>Mon, 14 Dec 2020 02:00:19 UTC +00:00}
so in order to find subscription_history.data
where subscription.id
is equal to 169lTASJ5wfsY3y3u
SubscriptionsHistory.where("data->'subscription'->>'id' = ?", '169lTASJ5wfsY3y3u')
Entire blog website and all the articles can be forked from this Github Repo