如何从Rails中的枚举值获取键名? [英] How to get key name from a enum value in Rails?
本文介绍了如何从Rails中的枚举值获取键名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的模型中有一个对应于数据库中列的枚举。
I have a enum in my Model that corresponds to column in the database.
该枚举看起来像:
enum efficency: { High: 0, Medium: 1, Low: 2 }
如何从值中获取密钥
例如,我的值为0,我需要获取值High。
For example, i have the value 0, i need to fetch the value High.
任何帮助将不胜感激。
推荐答案
Model.efficiencies.key(0) # => 'High'
用实际值替换 Model
模型的名称。
Replace Model
with the actual name of your model.
这篇关于如何从Rails中的枚举值获取键名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文