Last active
April 18, 2017 14:39
-
-
Save eboswort/86a82e663cacad326e9ff13a78aaeb3a to your computer and use it in GitHub Desktop.
json query examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
# in controller | |
@found = Shipment.search(params[:q]) | |
# in view | |
= search_form_for @found do |f| | |
= f.select :tracking_info_status_eq, Shipment.unique_tracking_statuses | |
# In the model you have to make your own ransacker | |
ransacker :tracking_info_status do |parent| | |
Arel::Nodes::InfixOperation.new('->>', | |
parent.table[:tracking_info], | |
Arel::Nodes.build_quoted('status')) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment