我真的很喜欢 Rails 4 的新枚举功能,但我想使用我的枚举
枚举状态:[:active, :inactive, :deleted]
在每个模型中。我找不到任何方式来声明例如 config/initializes/enums.rb
并包含每个模型
我是 Ruby on Rails
的新手,需要您的帮助来找到解决方案
最佳答案
使用ActiveSupport::Concern
此功能是为干燥
模型代码而创建的:
#app/models/concerns/my_enums.rb
module MyEnums
extend ActiveSupport::Concern
included do
enum status: [:active, :inactive, :deleted]
end
end
# app/models/my_model.rb
class MyModel < ActiveRecord::Base
include MyEnums
end
# app/models/other_model.rb
class OtherModel
include MyEnums
end
关于ruby-on-rails - Ruby on Rails 全局 ActiveRecord::枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25937298/