我将enum应用于以下属性:transparency
在两个不同的模型中使用相同的属性(带有枚举):Category
和Post
是否有可能在模型之间共享枚举值,以避免代码重复:
enum transparency: %w(anonymous private public)
您可以使用concern。
module HasTransparency
extend ActiveSupport::Concern
included do
enum transparency: %w(anonymous private public)
end
end
然后将其包含在模型中:
class Category < ActiveRecord::Base
include HasTransparency
....
end
使用关注点或模块的“正确方法”的替代方法,您可以仅引用另一个类的枚举。它对我来说非常有效:
enum same_values_than_other: SomeOtherClass.my_awesome_enum