0

How to search in nested jsonb column in Rails.

Model: Shop
jsonb column: shop_data

shop_data: {
  common_data: {
    "image_url" => "https://sample.com/img.jpg"
    "token" => "AOsa2123ASDasdaasasda"
    "uid" => ""
    "expires_at" => ""
  }
}

Wanna make a scopes that will checks for records where:

1. shop_data->common_data->expires_at IS NOT NULL
2. shop_data->common_data->image_url IS NULL

1 Answer 1

0
 1. scope :expired, -> { where("shop_data -> 'common_data' ->> 'expires_at' IS NOT NULL") }
 2. scope :null_image, -> { where("shop_data -> 'common_data' ->> 'image_url' IS NULL") }

source JSON functions in postgres

Hope it helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.