Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. We want to query out database, but not generate ActiveRecord models (which takes additional resources).
  2.  
  3. Since this report displays a LOT of rows of data we are aiming for speed.
  4.  
  5. ```ruby
  6. sql = Recruitment::Patient.order(reference: :desc).select(patient_attrs).to_sql
  7.  
  8. results = ActiveRecord::Base.connection.execute(sql).map do |row|
  9. Hash[patient_attrs.zip(row)].transform_values(&method(:format_value))
  10. end
  11.  
  12. def patient_attrs
  13. @patient_attrs ||= %i(identifier study_reference consented_on screened_on recruited_on)
  14. end
  15.  
  16. def format_value(value)
  17. # ...
  18. value
  19. end
  20. ```
  21.  
  22. `results` will be an `Array<Hash>`. You could make it a `Array<Struct>` if prefered.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement