one query for avis count

This commit is contained in:
simon lehericey 2021-04-23 14:56:17 +02:00
parent 5cfb4114ce
commit a4e813380a
2 changed files with 21 additions and 10 deletions

View file

@ -21,4 +21,17 @@ class Expert < ApplicationRecord
def self.by_email(email)
Expert.eager_load(:user).find_by(users: { email: email })
end
def avis_summary
if @avis_summary.present?
@avis_summary
else
query = <<~EOF
COUNT(*) FILTER (where answer IS NULL) AS unanswered,
COUNT(*) AS total
EOF
result = avis.select(query)[0]
@avis_summary = { unanswered: result.unanswered, total: result.total }
end
end
end