Rails and Json in PG

http://edgeguides.rubyonrails.org/activerecordpostgresql.html

    # db/migrate/20131220144913_create_events.rb
    create_table :events do |t|
      t.json 'payload'
    end

    # app/models/event.rb
    class Event < ApplicationRecord
    end

    # Usage
    Event.create(payload: { kind: "user_renamed", change: ["jack", "john"]})

    event = Event.first
    event.payload # => {"kind"=>"user_renamed", "change"=>["jack", "john"]}

    ## Query based on JSON document
    # The -> operator returns the original JSON type (which might be an object), whereas ->> returns text
    Event.where("payload->>'kind' = ?", "user_renamed")

Post Content