Skip to content

Instantly share code, notes, and snippets.

@mooreniemi
Created June 20, 2016 00:16
Show Gist options
  • Save mooreniemi/41dc899a534fa13f062e95b7f3702b5f to your computer and use it in GitHub Desktop.
Save mooreniemi/41dc899a534fa13f062e95b7f3702b5f to your computer and use it in GitHub Desktop.
class SatisfactionQuery
def self.execute
query = <<-SQL.strip_heredoc
SELECT pins.id AS pin_id, pins.satisfaction, procedures.name, surgeons.last_name,
dense_rank() OVER (
PARTITION BY procedure_id, surgeon_id
ORDER BY pins.satisfaction DESC
) AS sat_rank
FROM pins
INNER JOIN procedures
ON procedures.id = pins.procedure_id
INNER JOIN surgeons
ON surgeons.id = pins.surgeon_id
WHERE satisfaction IS NOT NULL;
SQL
results = []
ActiveRecord::Base.connection.select_all(query).each do |row|
results << OpenStruct.new(row) unless row.nil?
end
results
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment