Module: RandomFinder

Included in:
ActStatusCode, AdvanceDirectiveStatusCode, AdvanceDirectiveType, AdverseEventType, AllergyStatusCode, AllergyTypeCode, CodeSystem, ContactType, CoverageRoleType, EncounterLocationCode, EncounterType, Ethnicity, Gender, InsuranceType, IsoCountry, IsoLanguage, IsoState, LanguageAbilityMode, LoincLabCode, MaritalStatus, MedicationType, NoImmunizationReason, ProblemType, ProcedureStatusCode, ProviderRole, ProviderType, Race, Relationship, Religion, ResultTypeCode, RoleClassRelationshipFormalType, SeverityTerm, SnowmedProblem, Vaccine, ZipCode
Defined in:
lib/random_finder.rb

Overview

This module extends ActiveRecord::Base#find by permitting the user to efficiently select a random record.

class MyModel < ActiveRecord::Base
  extend RandomFinder
end
random_instance = MyModel.find :random

If there are no records from which to select, nil is returned.

Currently it's not possible to pass other options when finding a random instance.

Instance Method Summary (collapse)

Instance Method Details

- (Object) find(*args)



14
15
16
17
18
19
20
# File 'lib/random_finder.rb', line 14

def find *args
  if args == [:random]
    first :offset => rand(count)
  else
    super
  end
end