Created
August 30, 2009 15:01
-
-
Save hmans/178000 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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