mongoid验证数组至少有1个元素

时间:2014-07-05 02:11:58

标签: ruby-on-rails validation mongoid

我们有

class Event
  include Mongoid::Document
  field :categories, type: Array, default: []
end

如何验证事件对象在类别中至少包含1个元素?我应该使用自定义验证器吗?谢谢

1 个答案:

答案 0 :(得分:3)

class Event
  include Mongoid::Document
  field :categories, type: Array, default: []

  validates :categories, length: { minimum: 1 }
end

应该做的伎俩(http://guides.rubyonrails.org/active_record_validations.html#length