jamal
jamal

Reputation: 75

query consult JSON fields in ruby on rails

how to make a query in a field of JSON type to return only one key? For example I have this model ActivityPart below and I want to return the activity_json field just a key named "image".

ActivityPart < ApplicationRecord {
                   :id => :integer,
        :activity_type => :string,
        :activity_json => :json,
           :created_at => :datetime,
           :updated_at => :datetime,

Upvotes: 0

Views: 68

Answers (1)

Yurii Stefaniuk
Yurii Stefaniuk

Reputation: 1797

You can use following code:

activity_parties = ActivityPart.select("activity_json->'image' AS image")

And you will be able to access this column for each record

activity_parties.each { |party| party.image }

Upvotes: 1

Related Questions