Skip to content

Instantly share code, notes, and snippets.

@hmans
Created August 30, 2009 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmans/178000 to your computer and use it in GitHub Desktop.
Save hmans/178000 to your computer and use it in GitHub Desktop.
def recalculate!
self.number_of_questions = Match.connection.select_value("SELECT count(*) as c
FROM
responses as my_responses
LEFT JOIN
responses as their_responses
ON their_responses.user_id = #{user_b.id}
AND my_responses.question_id = their_responses.question_id
WHERE
my_responses.user_id = #{user_a.id}
AND
their_responses.user_id IS NOT NULL").to_i
self.number_of_same_answers = Match.connection.select_value("SELECT count(*) as c
FROM
responses as my_responses
LEFT JOIN
responses as their_responses
ON their_responses.user_id = #{user_b.id}
AND my_responses.answer_id = their_responses.answer_id
WHERE
my_responses.user_id = #{user_a.id}
AND
their_responses.user_id IS NOT NULL").to_i
self.score = sufficient? ? 100 * (number_of_same_answers.to_f / number_of_questions.to_f) : nil
save!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment