Best way to store Enum value in ActiveRecord and convert to string for display

前端 未结 5 1777
野的像风
野的像风 2021-02-02 17:05

I am trying to figure out what is the best way to store an enum value in activerecord but convert it to a \'title\' for display in an app.

I.E.

Review Enum:

5条回答
  •  北恋
    北恋 (楼主)
    2021-02-02 17:43

    there are a lot of posts about this issue, i guess that this points to most of them: http://thinkinginrails.com/2010/04/using-enums-for-constants/

    i think that this is an over engineered thing, that you don't need in a dynamically typed language like ruby.

    just use strings!

    you could then use that like:

    class Review < ActiveRecord::Base
      validates_inclusion_of :status, :in => ["UNREVIEWED", "REVIEWED", "FLAGGED"]
      def status
        read_attribute(:status)
      end
      def status= (value)
        write_attribute(:status, value)
      end
    end
    

提交回复
热议问题