TIL #1 : How to pluck jsonb fileds in Rails

1 min read

If you have a jsonb field in your Active record object and you want some nested value you can use pluck
Example: Lets say your Users model has a jsonb attribuite metadata which has a nested key location which in turn has city you can get the value of city like
Users.pluck("metadata -> 'location' -> 'city'") => ["London", "Liverpool"]
0
Subscribe to my newsletter
Read articles from Dhaval Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
