Created
August 5, 2010 03:43
-
-
Save JonKernPA/509193 to your computer and use it in GitHub Desktop.
Demonstrating various searches in mongomapper
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
class MessageLog | |
include MongoMapper::Document | |
# Attributes :::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
key :repeat_count, Integer, :default => 0 | |
key :patient_name, String, :default => 'Unknown', :index => true | |
key :patient_num, String, :default => 'Unknown', :index => true | |
key :patient_emr, String, :default => 'Unknown', :index => true | |
# Doctor info (number, name, group) | |
key :doctor, String, :default => 'Unknown', :index => true | |
... | |
# Scopes ::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
scope :by_emr, lambda {|emr| where(:patient_emr => emr)} | |
end | |
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
And searches like this in the controller that allow numbers as text | |
(from a UI search form): | |
... | |
def search | |
@message_logs = nil | |
case params[:type] | |
when "name" | |
@message_logs = MessageLog.paginate( :page =>params[:page], | |
'$where' => "function() { return | |
this.patient.match(/#{params[:finder]}/i); }") | |
when "patient_num" | |
@message_logs = MessageLog.paginate(:patient_num => | |
params[:finder], :page =>params[:page]) | |
when "emr_num" | |
@message_logs = MessageLog.by_emr(params[:finder], :page | |
=>params[:page]).paginate | |
when "doctor" | |
@message_logs = MessageLog.paginate( :page =>params[:page], | |
'$where' => "function() { return | |
this.doctor.match(/#{params[:finder]}/i); }") | |
when "control_id" | |
@message_logs = MessageLog.paginate( :control_id => | |
params[:finder], :page =>params[:page]) | |
end | |
end | |
... | |
And for the stats, I search with a straight-up number: | |
@num_repeated = MessageLog.count( :repeat_count.gt => 0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment