Skip to content

Instantly share code, notes, and snippets.

View eboswort's full-sized avatar

Eli Bosworth eboswort

  • Keko Box
  • New York City
View GitHub Profile
@eboswort
eboswort / nested_array_json_queries_with_view.rb
Last active April 18, 2017 15:07
Nested array json queries with view
# Here's the migration to pull each shipment_event into its own row in a view
class CreateTrackingEventsView < ActiveRecord::Migration
def change
sql = %{
SELECT
json_array_elements(tracking_info->'shipment_events') as info,
number as shipment_number
FROM shipments where (tracking_info->'shipment_events') IS NOT NULL
}
@eboswort
eboswort / nested_array_json_queries.rb
Last active April 18, 2017 14:42
Nested array json queries
# Here's an example tracking json object with shipment_events
example_tracking_json = {
"status": "delivered",
"shipment_events":[
{
"name":"BILLING INFORMATION RECEIVED",
"time":"2015-07-23T17:22:59.000Z", ...
}, ...
{
"name": "THE RECEIVER WAS NOT AVAILABLE AT THE TIME OF THE FIRST DELIVERY ATTEMPT.",
@eboswort
eboswort / json_query_examples.rb
Last active April 18, 2017 14:39
json query examples
# We pulled out the scheduled delivery date to render in our order status page
shipment.tracking_info['scheduled_delivery_date']
# We created some scopes to keep track of shipments in different stages of delivery
scope :delivered, -> { where("tracking_info->>'status' = 'delivered'") }
# Syntax could get a little ugly with slightly more complicated queries
Shipment.where("promised_arrival_date < (tracking_info->>'actual_delivery_date')::date")
# We got it working with ransack, a gem we use for creating search forms

Keybase proof

I hereby claim:

  • I am eboswort on github.
  • I am eboswort (https://keybase.io/eboswort) on keybase.
  • I have a public key ASDkUdR5wcT0ivrk2ebEc2DGsmO_1r0Qc5mUMNGcO7yfrQo

To claim this, I am signing this object: