In Rails, how should I implement a Status field for a Tasks app - integer or enum?

后端 未结 6 511
南笙
南笙 2021-01-30 03:24

For a Rails 3.0 Todo app, I have a Tasks model with a Status field. What\'s the best way to store the Status field data (field type) and still display a human-r

6条回答
  •  失恋的感觉
    2021-01-30 03:48

    I prefer to store "normal", "active", .. "completed" as string in the table because:

    • it's self documentation (for example, someone may look at the data but never read the Rails source code)
    • there is little (if not no) performance penalty
    • it is (still) easy to do i18n by means of Rails virtual attribute in the model or whatever layer in other languages

    These days, I tend to decouple Rails constants from the database as much as I can. There are always some PHP/MSSQL/??DBA folks around us (who may not love Rails as much as we do ;-)

    So, the answer is not integer nor enum (but a varchar ;-)

提交回复
热议问题