【问题标题】:Rails 4. Model Predefined valuesRails 4.模型预定义值
【发布时间】:2014-07-31 16:23:25
【问题描述】:

我正在创建 Rails API。我有一个具有预定义值的模型

class CreateMessages < ActiveRecord::Migration
   def change
       create_table :messages do |t|
           t.string :status #set [delivered, pending, error]
       end
   end
end

属性status可能只有3个值delivered, pending, error。如何为模型中的字段 status 添加验证

【问题讨论】:

标签: ruby-on-rails validation ruby-on-rails-4 model


【解决方案1】:

将此添加到您的模型中

validates_inclusion_of :status, :in => %w( delivered pending error ) 

更多信息here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-02
    • 2014-12-21
    • 2015-12-10
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    相关资源
    最近更新 更多