I need to create a customer model that is mostly a measure form.
It will record some general info about the user (name, age, sex etc) and a lot of measures (height, weight, neck cisrcumference, waist etc.).
The app is targeted to international users so some will prefer to use centimeters/kilograms, other will prefer to use inches/pounds.
I think the best approach would be to store all the data in a consistent format in the database (for example metric system) and convert everything in the controller side.
Is there a canonical way (or an existing gem) to obtain this in rails? If not I would create a preferred_measure_type field in the model and then render all the views in the user preference.
Also I heard it’s wise to avoid floats and decimals and store the data in integer (in my scenario millimeters and grams) and then do all the conversion on the application side. Is that a good advice?
Thank you
It’s always best to store data in canonical units and then convert for display purposes. Use the smallest measure that will capture the detail you require without overflowing the capacity of the column. For example, millimeters might be appropriate for shirt sizes, but might prove inadequate for measuring highways.
As for floats, these are problematic mostly because of rounding issues. When dealing with currency, even minor discrepancies are going to cause problems. $2.00 plus $2.00 should always equal $4.00, not $3.9999917237, which might get inadvertently rounded to $3.99 leaving a penny “missing”.
Where such precision and consistency is not required, floats are fine. The thing to remember is that a floating point number is always an approximation of any value you give it, and the accuracy diminishes as the number gets larger. That is, a
floatin the billions will not accurately reflect changes in the thousandths, there’s not enough precision to represent it.So where possible, use a fixed-place system, for instance by using a specific unit. When necessary, use a floating-point value but always be sure to round these off properly for display and remember that they are always approximations and never exact.