作者在 2019-05-24 00:36:37 发布以下内容
假设这么一个Model
class Order < ApplicationRecord
enum type: {'支付宝充值': 1, '微信充值': 2, '后台手工加值':3, '后台手工减值':4, '提现':5, '发布问题减值':6, '答案被选中加值':7}
end
那么可以进行下面这些枚举操作,首先是对象的操作
[1] pry(main)> order = Order.first
=>
[2] pry(main)> order.type
=> "后台手工减值"
[3] pry(main)> order.read_attribute_before_type_cast(:type)
=> 4
下面是对类的一些操作
[1] pry(main)> Order.types
=> {"支付宝充值"=>1, "微信充值"=>2, "后台手工加值"=>3, "后台手工减值"=>4, "提现"=>5, "发布问题减值"=>6, "答案被选中加值"=>7}
[2] pry(main)> Order.types.keys
=> ["支付宝充值", "微信充值", "后台手工加值", "后台手工减值", "提现", "发布问题减值", "答案被选中加值"]
[3] pry(main)> Order.types.values
=> [1, 2, 3, 4, 5, 6, 7]
[4] pry(main)> Order.types[:后台手工减值]
=> 4
[5] pry(main)> Order.types.key(4)
=> "后台手工减值"