Rails, string or integer values in status-column in db
I'm having an event-model that has a status attribute, and wonder 开发者_Python百科if I should make it into a string or integer field. The event can have three different statuses, "gathering", "active" or "closed". If I should go with the integer solution, should I define a constant somewhere making it possible to just use CLOSED insted of "2" (and where should I put this constant??)
Thanks in advance!
I would recommend using an integer for two main reasons.
- Most databases can query and index integer columns faster than strings.
- It takes up less space on disk, and over the network when querying large data sets.
Weather you choose strings or integers, you should define them as constants and reference them only through their constants. You should never use the values directly.
http://en.wikipedia.org/wiki/Magic_string_(programming)
精彩评论