Ruby on Rails全局ActiveRecord ::枚举 [英] Ruby on Rails Global ActiveRecord::Enum
本文介绍了Ruby on Rails全局ActiveRecord ::枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
枚举状态:[:active,:inactive, :删除]
在每个模型。我找不到任何方式如何在 config / initializes / enums.rb
中声明,并包括每个模型
我非常新的 Ruby on Rails
,需要您的帮助才能找到解决方案
解决方案
使用 ActiveSupport :: Concern
为 dry
创建的此功能创建型号代码:
#app / models / concern / my_enums.rb
module MyEnums
extends ActiveSupport :: Concern
包含
枚举状态:[:active,不活动,已删除]
end
end
#app / models / my_model.rb
class MyModel< ActiveRecord :: Base
包括MyEnums
end
#app / models / other_model.rb
class OtherModel
include MyEnums
end
I really like Rails 4 new Enum feature, but I want to use my enum
enum status: [:active, :inactive, :deleted]
in every model. I cannot find any way how to declare for example in config/initializes/enums.rb
and include every model
I'm very new in Ruby on Rails
and need your help to find solution
解决方案
Use ActiveSupport::Concern
this feature created for dry
ing up model codes:
#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全局ActiveRecord ::枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文