Rails Database Design: Use strings or integer?

折月煮酒 提交于 2019-12-23 23:23:42

问题


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.


回答1:


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.




回答2:


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/



来源:https://stackoverflow.com/questions/12786435/rails-database-design-use-strings-or-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!