3

Suppose I have a rails tables containing information chosen from a set number of options. For example, a field named sex could be either Male or Female. A field named Bodytype will be either slim, curvy, etc. My question is, what is better practice, to store those values as integers or strings? In the first case, of course, the integers will be converted into text (In the controller?).

Thanks for all your help.

3
  • 4
    Gender today is not a black and white subject. Use a floating point value between 0 and 1. :D Commented Oct 8, 2012 at 17:23
  • haha, I will take it under consideration Commented Oct 8, 2012 at 17:24
  • Somewhat biased article but good info: codinghorror.com/blog/2008/07/… at least to see what questions/concerns are being raised by both parties. Commented Oct 8, 2012 at 20:37

2 Answers 2

3

If are you are not storing millions of records, storing them as strings is fine. What you are loosing is ability to quickly update these categories, but if they are pretty much static and not going to change over time, it shouldn't matter. Sex should probably be called gender and Bodytype body_type.

Sign up to request clarification or add additional context in comments.

6 Comments

A properly normalized database would use the smallest possible data type able to uniquely represent the total number of required possibilities, and would use another table to look up human readable names for those values.
Properly normalized databases are not always the best solution. There are a lot of other things to consider especially in rails development.
which normal form says to use the smallest data type?
There seem to be conflicting views. How do I know which one is right?
It's a philosophical debate. You can aways normalize/denormalize table later. Do you want to deal with joins, extra models, extra code? How large is your database going to grow, are you reading data more often then writing data? For complex concepts like address, it's totally fine to store it in a different table, for simple concepts like gender and body type it makes absolutely no sense.
|
1

You should always use an index to identify attributes within your tables. So for example you tables will look like this

Gender Table
  id | sex     
  1  | Female
  2  | Male

Figure Table
  id | body_type
  1  | slim
  2  | curvy

You then reference those values based on the id

http://use-the-index-luke.com/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.