Share enum declaration values with multiple attributes

后端 未结 3 1797
梦谈多话
梦谈多话 2021-02-03 22:41

I want to have a class with several attributes that saves weekdays with numeric values.

summary_weekday    :integer
collection_weekday :integer

3条回答
  •  走了就别回头了
    2021-02-03 23:09

    As of Rails 5.0 you can use the _prefix or _suffix options when you need to define multiple enums with same values. If the passed value is true, the methods are prefixed/suffixed with the name of the enum.

    class Invoice < ActiveRecord::Base
      enum verification: [:done, :fail], _prefix: true
    end
    

    It is also possible to supply a custom prefix.

    class Invoice < ActiveRecord::Base
      enum verification: [:done, :fail], _prefix: :verification_status
    end
    

提交回复
热议问题