TimmyOnRails
TimmyOnRails

Reputation: 327

Rails Database Design: Use strings or integer?

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.

Upvotes: 3

Views: 1288

Answers (2)

Andre Dublin
Andre Dublin

Reputation: 1158

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/

Upvotes: 1

iouri
iouri

Reputation: 2929

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.

Upvotes: 3

Related Questions