如何指定和验证在轨枚举? [英] How do i specify and validate an enum in rails?

查看:145
本文介绍了如何指定和验证在轨枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个模型参加,将有一个状态栏,此状态栏将只有几个它的值。 STATUS_OPTIONS = {:是的,不,:也许}

I currently have a model Attend that will have a status column, and this status column will only have a few values for it. STATUS_OPTIONS = {:yes, :no, :maybe}

1)我不知道我怎么可以验证该用户将一个参加过吗?基本上在java中的枚举,但我怎么能做到这一点的轨道?

1) I am not sure how i can validate this before a user inserts an Attend? Basically an enum in java but how could i do this in rails?

推荐答案

创建你想要的选项全球访问的磁盘阵列,然后验证您的身份列的值:

Create a globally accessible array of the options you want, then validate the value of your status column:

class Attend < ActiveRecord::Base

  STATUS_OPTIONS = %w(yes no maybe)

  validates :status, :inclusion => {:in => STATUS_OPTIONS}

end

您便可以通过访问可能的状态参加:: STATUS_OPTIONS

这篇关于如何指定和验证在轨枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆