Created
August 2, 2013 15:50
-
-
Save flarnie/6140993 to your computer and use it in GitHub Desktop.
This is our instructor's version of the validator, using .persisted? to check that the current record has already been saved and then taking it out of the 'duplicates_responses' set.
This file contains hidden or 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 respondent_has_not_already_answered_question | |
duplicate_responses = self | |
.question | |
.responses | |
.where("responses.respondent_id = ?", self.respondent_id) | |
if persisted? | |
duplicate_responses = | |
duplicate_responses.where("responses.id != ?", self.id) | |
end | |
unless duplicate_responses.empty? | |
errors[:respondent_id] << "cannot vote twice for question" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment