Inheritance with different type of values in a database

46 views Asked by At

I have a API interface where it takes in patient's LabTestResult and saves it in the database. The lab test result's result column can be either a string (long text from a doctor), an integer value, or +/- (I'm gonna use boolean for it).

Currently I have a database with three columns (text_result, integer_result, sign_result) and for each labtestresult one of these is field.

First of all, is this the right way of doing it? or do I need to have three different tables with only the result value being of different types?

I'm also thinking of using inheritance, have a main model LabTestResult and have three children models TextLabTestResult, IntegerLabTestResult, SignLabtestResult.

So now my question is when the request comes to the controller (API) should i have a logic there that if the result is a string do TextLabTestResult.new or should I have the logic in the parent model LabTestResult and have a case statement, where if we have a string do TextLabTestResult, etc.

I feel like there is a design pattern that describes this, and I might be totally missing something here, and there could be a better design all together.

1

There are 1 answers

0
AJcodez On

When designing anything, always go with the simplest option. If the result can be a bunch of text, a number, or a + or -, then represent it as text.

It doesn't even sound like you need a result table at all. Just have the result be part of the lab_tests table.

rails g model LabTest result:text