Skip to content

Instantly share code, notes, and snippets.

@kasima
Created April 25, 2011 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kasima/941464 to your computer and use it in GitHub Desktop.
Save kasima/941464 to your computer and use it in GitHub Desktop.
Mongo Office Hours
class DiagnosticReport
include Mongoid::Document
embeds_many :marker_values
end
class MarkerValue
include Mongoid::Document
embedded_in :diagnostic_report, :inverse_of => :marker_values
embeds_many :test_results
referenced_in :marker
scope :for_health_category, lambda { |health_category| where(:marker_id.in => health_category.marker_positions.map(&:marker_id)) }
end
class Marker
include Mongoid::Document
embeds_many :risk_ranges
# fields ...
end
class RiskRange
include Mongoid::Document
embedded_in :marker
# fields ...
end
class HealthCategory
include Mongoid::Document
embeds_many :marker_positions
# fields...
end
class MarkerPosition
include Mongoid::Document
field :position
embedded_in :health_category
referenced_in :marker
end
# QUERIES
# Sorted list of marker values by health category
marker_ids = health_category.marker_positions.map { |mp| mp.marker_id }
matching_values = marker_values.where(:marker_id.in => marker_ids)
matching_values.sort! { |a, b| health_category.marker_positions.where(:marker_id => a.marker_id).position <=> health_category.marker_positions.where(:marker_id => b.marker_id).position}
# Find all health categories for which marker values exist in a given diagnostic report
matching_categories = HealthCategory.all.reject { |hc| diagnostic_report.marker_values.for_health_category(hc).empty? }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment