ruby-on-rails - 跨模型共享属性枚举
问题描述
我正在使用 Enumerize gem,我想创建一个在两个模型之间共享的 Enum。
我的 Enum 模型如下所示:
class StudyTypeEnum < ApplicationRecord
extends Enumerize
enumerize :studytype, in: {:full_time, :part_time}
end
然后我只是将它包含在其他模型中
class Course < ApplicationRecord
include StudyTypeEnum
...
我现在不确定如何创建迁移,是否需要在 StudyTypeEnum 和 Course 模型中创建 StudyType 列?
解决方案
我会担心这种行为。
在文件中:app/models/concerns/enumerable_study.rb
module EnumerableStudy
extend ActiveSupport::Concern
extends Enumerize
included do
enumerize :studytype, in: {:full_time, :part_time}
end
end
然后如果您的任何模型需要该字段,只需执行以下操作:
例如在文件中:app/models/course
class Course < ApplicationRecord
include EnumerableStudy
end
推荐阅读
- python - 将“亚洲/加尔各答”时区日期时间转换为自纪元以来的毫秒数时出错
- python - scrapy:添加一些数学条件
- json - 在 postgres sql 中拆分 JSON 列
- sql - 在Oracle中将连字符分隔的字符串拆分为行
- vue.js - 图标显示不正确 - ag 网格
- list - 需要解释基本的 do 块语法
- hierarchical-clustering - 如何使用 fviz_nbclust 可视化来自戴安娜的最佳集群数量
- c++ - 现代 Opengl 线点画问题
- android - NoSuchMethodError:在 null 上调用了方法“[]”| 解析 JSON
- twitter-bootstrap - 如何使用引导程序制作包含 3 张卡的响应式行?