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

后端 未结 6 510
南笙
南笙 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:54

    The easiest thing to do would be to just store the actual strings in the field instead of adding another table. Depending on your point of view this is either a bad idea as your database will not be sufficiently normalized or a great idea as your app is now more efficient for being normalized.

    If you opt to not do that and to keep the values in a separate table; you need to setup the relationships in the model.

    class Task < ActiveRecord::Base
        has_one :status
    end
    
    class Status < ActiveRecord::Base
        belongs_to :tasks
    end 
    

    Then in your view you can reference the value by:

    <%= task.status %>
    

提交回复
热议问题