在模型之间共享枚举声明值

我在以下属性上应用枚举 : transparency

相同的属性(使用枚举)用于两个不同的模型: CategoryPost

是否可以在模型之间共享枚举值,以避免代码重复:

 enum transparency: %w(anonymous private public) 

你可以使用一个问题 。

 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