Skip to content

Instantly share code, notes, and snippets.

@flarnie
Created August 2, 2013 15:50
Show Gist options
  • Save flarnie/6140993 to your computer and use it in GitHub Desktop.
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.
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