【问题标题】:How to validate a Rails model against an enum?如何针对枚举验证 Rails 模型?
【发布时间】:2012-08-02 03:15:47
【问题描述】:

类似于 mongoose for nodejs 有一个枚举验证器,其中字段的值必须是预定义数组中的值之一,Rails 中的 mongoid 模型如何复制这种行为?

即。

field :category, type: String --> must be in one of [categoryA, categoryB, categoryC]

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 mongoid rails-models


    【解决方案1】:

    你可以用这个:

    validates :category, :inclusion => { :in => ["categoryA", "categoryB", "categoryC"] }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 2010-12-31
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      相关资源
      最近更新 更多