From a user perspective, you shouldn't even display an option for a user to vote if they have already voted on a story. Having a validation run before_save is a good way to make sure it doesn't happen, but the user shouldn't get to that point in the first place.
So, add a method to the User model (or alternatively, the Story model)
Code:
def has_voted?(story_id)
# check to see if the user has voted on the story
# something like
!self.votes.find_by_story_id(story_id).nil?
end
which will return true if the user has voted on story_id.
Then, create a helper method for your voting display
Code:
def show_vote(story,user)
[... code that display a voting link ...] unless user.has_voted?(story.id)
end
Code isn't tested, but it should give the gist of what I'm getting at.
Bookmarks