Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created September 9, 2019 10:17
Show Gist options
  • Select an option

  • Save krisleech/46eef027b0a286d7c5954d4284fe5c95 to your computer and use it in GitHub Desktop.

Select an option

Save krisleech/46eef027b0a286d7c5954d4284fe5c95 to your computer and use it in GitHub Desktop.
RAW SQL ACTIVERECORD REPORTS

We want to query out database, but not generate ActiveRecord models (which takes additional resources).

Since this report displays a LOT of rows of data we are aiming for speed.

sql = Recruitment::Patient.order(reference: :desc).select(patient_attrs).to_sql

results = ActiveRecord::Base.connection.execute(sql).map do |row|
  Hash[patient_attrs.zip(row)].transform_values(&method(:format_value))
end

def patient_attrs
  @patient_attrs ||= %i(identifier study_reference consented_on screened_on recruited_on)
end

def format_value(value)
  # ...
  value
end

results will be an Array<Hash>. You could make it a Array<Struct> if prefered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment