How to Properly Configure Rails 4.1 Enums in ActiveAdmin

后端 未结 3 668
日久生厌
日久生厌 2021-02-02 07:48

I\'ve got a Rails 4.1 app in which I use an enum to represent the privacy level of an object.

In my schema:

t.integer \"privacy_level\", default: 0
         


        
3条回答
  •  一向
    一向 (楼主)
    2021-02-02 08:19

    Building off of Jack's answer, here's what worked for me. Say your ActiveRecord model is Tweets:

    f.input :privacy_level, as: :select, collection: Tweet.privacy_levels.keys
    

    Key things to note here:

    • your ActiveRecord has a useful dictionary (available at enum_name.pluralize) of enum keys to values.
    • using strings (and ignoring the underlying integer representation) makes it easier to write to the enum value.

提交回复
热议问题