views:

68

answers:

1

I think the way I've modelled my app is a bit fishy and i need to rejig things, im just not sure how. I've already re-jigged and refactored before. It took a long time ( I'm a beginner ) and I'm hesitant to it again in case i head off in the wrong direction again.

Basic Idea, user can submit an answer, another user can mark it correct or incorrect. If incorrect they have to write the correct answer. Users can view their and everybody else's correct and incorrect answers.

So I did it this way

class Answer
  has_one: correction
end

class Correction
  belongs_to :answer
end

when a user marks an answer as correct, I set checked_at:DateTime and checked_by_id:integer on the Answer object to keep track of who checked the answer and when.

For incorrect answers I create a correction object which holds the correct answer and again checked_by and checked_at details.

I don't like this because I have checked_by and checked_at in both models. It just doesn't sit right.

Possible solutions are:

Create a third model such as VerifiedAnswer and move the checked_by/at attributes to that. It will handle the situtation where an answer is marked correct.

Or are these models thin enough (they dont have any other attributes) that I can just have one model ( Answer ) that has all the attributes to store all this information?

+1  A: 

I would make an answer corrected by another answer. That way, you can keep correcting the new answer.

This could be done by specifying:

class Answer
  belongs_to :correction, :class_name => "Answer"
end

Note that this will mean that any existing answers with corrections will have data set up incorrectly (since the correction_id is currently pointing to a separate table). You would need to clear out your database for this to work.

Daniel Heath
i was thinking of something like this but what what about in the case where an answer is actually correct?
adam
in that case, this association would be empty, and answer.correction would be nil. A belongs_to relation is not required
Faisal
thanks Faisal - but id need to know who checked it as ok which would mean id need to keep something like checked_by_user_id on the original answer model. So sometimes it would be nil and id have a correction object and other times it wont and I wont have a correction object. I wasn't sure if this was the cleanest way of implementing this.
adam
I would suggest using:checked_by_user_id and correction_id.Then, define a 'needs_checking' method which returns true if they are both false.Alternatively, a 'correct' method which returns true, false, or nil (depending on whether it is known to be correct, known to be wrong, or not yet corrected).
Daniel Heath
sounds like a good idea. Thanks for the info!
adam