Rails:在模型之间共享枚举声明值 [英] Rails: Share enum declaration values between models

查看:27
本文介绍了Rails:在模型之间共享枚举声明值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对以下属性应用 enum:透明度

I'm applying enum on the following attribute: transparency

相同的属性(带有枚举)用于两种不同的模型:CategoryPost

The same attribute (with enum) is used in two different models: Category and Post

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

Is it possible to share the enum values between models, to avoid code duplication:

enum transparency: %w(anonymous private public)

推荐答案

您可以使用 关注.

module HasTransparency
  extend ActiveSupport::Concern
  included do
    enum transparency: %w(anonymous private public)
  end
end

然后将其包含在您的模型中:

Then include it in your models:

class Category < ActiveRecord::Base
  include HasTransparency

  ....
end

这篇关于Rails:在模型之间共享枚举声明值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆