robodisco
robodisco

Reputation: 4282

Rails - Beginner wants feedback on how they've modeled their app and how to do it better

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?

Upvotes: 1

Views: 116

Answers (1)

Daniel Heath
Daniel Heath

Reputation: 414

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.

Upvotes: 1

Related Questions